Data Structures & AlgorithmsVery hardTCS Digital · Product companies
What is the maximum sum of a non-empty subarray in the circular array [8, -8, 9, -9, 10, -11, 12], where the subarray may wrap from the end to the start?
- A. 22
- B. 12
- C. 20
- D. 39
Correct answer: A. 22
The best non-wrapping subarray is 12 (Kadane). With wrapping, answer = total - minimum subarray = 11 - (-11) = 22, from the wrapped block 12, 8, -8, 9, -9, 10. Summing all positives (39) is not a contiguous block.
More Coding Interview Problems questions
- What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?
- What is the minimum number of coins needed to make an amount of 18 using unlimited coins of denominations {1, 6, 10}?
- The array is [3, 3, 4, 4, 5, 1, 2, 2, 6, 3, 7]. What is the length of its longest non-decreasing (not strictly increasing) subsequence?
- For the circular array [5, -3, 5, -2, 6, -8, 4], what is the maximum sum of a non-empty contiguous subarray when the subarray is allowed…
- A robot moves only right or down from the top-left to the bottom-right cell of this 6×6 grid, where # is a blocked cell and . is free…
- For the array [4, 5, 0, -2, -3, 1, 7, -9, 6] and k = 5, how many non-empty contiguous subarrays have a sum divisible by k?