ProgrammingHardTCS NQT · Accenture
The greedy below repeatedly increases the smallest element of the array [3, 1, 2] by 1, three times in total. What is the minimum value of the array printed after the three increments?
The greedy below repeatedly increases the smallest element of the array [3, 1, 2] by 1, three times in total. What is the minimum value of the array printed after the three increments?
for k = 1 to 3
find the index of the minimum; increase that element by 1
end for
Print minimum of the array- A. 1
- B. 9
- C. 2
- D. 3
Correct answer: D. 3
Increments take 1 to 2, then the new minimum 2 to 3, then the remaining 2 to 3, giving [3,3,3] with minimum 3. Incrementing the maximum leaves minimum 1, printing the sum gives 9, and stopping after two increments leaves minimum 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For the function gcd(a, b) written by the procedure below, what is printed for gcd(252, 105)? (The procedure uses repeated remainder…
- The Fibonacci numbers F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) satisfy gcd(F(m), F(n)) = F(gcd(m, n)). The procedure below uses this…
- The procedure below computes the last digit of 7^100 by starting from d = 1 and multiplying by 7, keeping only the last digit, once for…
- The number of ways to write 20 as p + q with p and q prime and p <= q is counted by the procedure below, which tries p = 2 to 10 and sets…
- For n = 1 to 100, the procedure below counts how many n satisfy that nn ends with the digit 1. What is printed? count = 0 for n = 1 to 100…
- The array a = [2, 3, 4, 1, 2, 3, 9] is scanned for its longest run of consecutive positions where each element is exactly one more than…