Data Structures & AlgorithmsHardGATE CS · TCS NQT
What is the maximum product over all non-empty contiguous subarrays of [2, 3, -2, 4, -1, -5, 0, 2]?
- A. 49
- B. 48
- C. 4
- D. 24
Correct answer: B. 48
Track both the running maximum and minimum product, because a negative value can turn the minimum into a maximum. A single running maximum misses cases where two negatives later make a large product from a previously minimal product.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many distinct values in [3, 3, 4, 2, 4, 4, 2, 4, 4] occur strictly more than n/3 times (n = 9)?
- How many non-empty subarrays of [1, 2, 1, 2, 3, 3, 1] contain every value an even number of times?
- Flowers bloom on the days [1, 10, 3, 10, 2]. You need to make 2 bouquets, each from 2 adjacent bloomed flowers (flowers may only be used…
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1, 1], you may flip exactly one 0 to 1 (or flip none). What is the length of the longest run of…
- How many index pairs i < j in [4, 9, 1, 7, 3, 8, 2] have a sum s with 6 <= s <= 10?
- How many contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?