Data Structures & AlgorithmsVery hardGATE CS
For each index i of [4, 7, 3, 8, 2, 2, 9, 1, 6, 5, 10, 3], let g(i) be the index of the first element to the right that is strictly greater than a[i] (and g(i) = i if none exists). What is the sum of (g(i) - i) over all i?
- A. 22
- B. 86
- C. 18
- D. 15
Correct answer: C. 18
Using a monotonic stack the next-greater indices give distances whose total is 18. The sum of the next greater values is 86, a different quantity.
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
- An array of 15 elements is rotated right by 6 positions using three in-place reversals (reverse the whole array, then reverse the first 6…
- What is the value of the postfix expression 5 3 2 + 4 - 6 2 / + (integer division)?
- A circular queue is kept in an array of size 7. The front index is 5 and the queue currently holds 4 elements. At which index will the…
- What is the postfix form of the infix expression a + b c - d / e?
- A singly linked list has 10 nodes numbered 1 to 10. Slow and fast pointers both start at node 1; the loop is while (fast != NULL &&…
- What is the maximum number of element comparisons a binary search (one three-way comparison per step) makes on a sorted array of 100…