Data Structures & AlgorithmsHardGATE CS · competitive programming
In the circular array [1, 5, 3, 4, 2], what is the next greater element of the value 2 at index 4, when the search wraps past the end?
- A. 0
- B. 1
- C. 5
- D. 4
Correct answer: C. 5
Wrapping checks index 0 (value 1, not greater) and then index 1 (value 5, the first greater value). A search that does not wrap would wrongly report that no greater element exists.
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
- A window of length 3 slides over [1, 3, −1, −3, 5, 3, 6, 7]. What is the sum of the maximum of each window?
- Treat [5, −3, 5] as a circle. What is the maximum sum of a non-empty contiguous subarray, where the subarray may wrap around the end?
- Ten people stand in a circle numbered 1 to 10. Counting from person 1, every 3rd person is eliminated and removed, and counting continues…
- The same rule, with every 3rd person eliminated, applies to n = 41 people. Who survives (1-based)?
- How many permutations of 1..5 can be produced by pushing 1, 2, 3, 4, 5 in order onto a stack and popping at any moment?
- How many pairs i < j in [2, 7, 3, 5, 8, 4] have A[i] + A[j] divisible by 3?