ProgrammingHardInfosys · Cognizant
For the array a = [7, 2, 4, 9, 1, 5] the procedure below computes the sum of the minimum of every contiguous window of length 2 (windows start at index 0 to n-2). What is printed?
For the array a = [7, 2, 4, 9, 1, 5] the procedure below computes the sum of the minimum of every contiguous window of length 2 (windows start at index 0 to n-2). What is printed?
total = 0
for i = 0 to n - 2
total = total + min(a[i], a[i+1])
end for
Print total- A. 34
- B. 9
- C. 10
- D. 23
Correct answer: C. 10
The pairwise minima are 2, 2, 4, 1, 1, so the total is 10. Using maxima gives 34, stopping one window early gives 9, and adding the first elements gives 23.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The array a = [3, 1, 2, 4] is sorted in ascending order by selection sort, and the procedure below counts the total number of element…
- Array a = [1, 2, 3, 5, 4] is checked by the procedure below, which performs bubble sort with an early-exit flag (a pass with no swaps ends…
- Array a = [3, 8, 5, 6, 7] has its odd values summed by the procedure below, and the result is printed. What is printed? total = 0 for each…
- Array a = [0, 2, 2, 3, 5]. The procedure below counts the positions i (0-based) where a[i] equals i. What is printed? count = 0 for i = 0…
- For the array a = [1, 2, 3, 4, 5, 6], the procedure below counts the contiguous subarrays of length 3 whose sum is divisible by 3. What is…
- Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints…