Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Array a = [4, 7, 1, 8, 3, 6, 5, 2]. What is the largest sum of a contiguous subarray that contains exactly two odd numbers?
- A. 20
- B. 24
- C. 27
- D. 32
Correct answer: B. 24
Group prefixes by their odd-count so far: a subarray has exactly two odd numbers when its end and start prefixes differ by two in odd-count. For each end, the best start is the smallest prefix among the valid ones, which a running minimum over the counts provides.
More Coding Interview Problems questions
- Array a = [2, 5, 1, 6, 3, 8, 4]. How many ordered pairs of distinct positions (i, j) satisfy a[j] >= 2 a[i]?
- From the values [4, 9, 1, 12, 7, 3, 10, 6], keep as many as possible so that in the kept set the maximum is at most twice the minimum. How…
- Seven people weigh [3, 5, 3, 4, 2, 6, 1] (kg). A boat carries at most two people and at most 7 kg in total. What is the minimum number of…
- Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?
- Four tasks each have (time, value): (3,2), (2,5), (4,4), (5,3). Choose any subset of tasks with total time at most 9 to maximise total…
- In how many ways can a set of 6 distinct people be partitioned into groups so that every group has an even number of people (groups are…