ProgrammingVery hardTCS NQT · Accenture
Array a = [5, -3, 5] is treated as circular. The maximum circular subarray sum (a contiguous block that may wrap around the end) is found by taking the larger of the best ordinary subarray sum and (total sum minus the smallest ordinary subarray sum). What is printed?
Array a = [5, -3, 5] is treated as circular. The maximum circular subarray sum (a contiguous block that may wrap around the end) is found by taking the larger of the best ordinary subarray sum and (total sum minus the smallest ordinary subarray sum). What is printed?
ordinary max = Kadane max; ordinary min = Kadane min total = sum of a Print max(ordinary max, total - ordinary min)
- A. 7
- B. 5
- C. 10
- D. 2
Correct answer: C. 10
The ordinary best is 7 (5, -3, 5), but wrapping gives 5 + 5 = 10 after removing the middle -3, which is the answer. A non-circular Kadane gives 7, the largest element gives 5, and adding the largest and smallest elements gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Sorted array a = [1, 3, 6, 6, 6, 9]. The lower-bound binary search below finds the first index whose value is at least x = 6. What is…
- Array a = [2, 4, 6, 8, 10]. The loop below sums i a[i] over the 0-based indices i. What is printed? total = 0 for i = 0 to 4 total = total…
- The array [0, 1, 0, 3, 12] has its zeros moved to the end while keeping the order of the other values, by a two-pointer pass that writes…
- 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…
- 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…