Data Structures & AlgorithmsVery hard
A subarray's average is at least K exactly when which quantity is non-negative for that subarray?
- A. The sum of (a[i] − K) over the subarray.
- B. The sum of a[i] alone.
- C. The product of (a[i] − K).
- D. The maximum of a[i] minus K.
Correct answer: A. The sum of (a[i] − K) over the subarray.
Average ≥ K is equivalent to sum ≥ K × length, which is sum of (a[i] − K) ≥ 0. That transformation turns an average test into a prefix-sum test.
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
- Convert the infix expression a+b(c-d)/e to postfix with the shunting-yard algorithm (operators '+', '-', '', '/' with '' and '/' binding…
- A 3-litre jug and a 5-litre jug start empty and you can fill either, empty either, or pour one into the other until one is full or empty…
- Reverse the sublist between positions 3 and 6 (1-based, inclusive) of the list 1, 2, 3, 4, 5, 6, 7, 8. What value is at position 4 of the…
- Rotate the list 1, 2, 3, 4, 5, 6, 7 to the right by 10 positions (each rotation moves the last node to the front). What value is at the…
- Stable partition of the list [1, 4, 3, 2, 5, 2] around x = 4 puts every node with value < 4 first, then every node with value >= 4…
- Asteroids move in a row; positive values move right and negative values move left, and when two meet the one with the smaller absolute…