GATE Computer ScienceHardGATE CS
The standard next_permutation algorithm (find the rightmost i with a[i] < a[i+1], swap a[i] with the rightmost larger a[j] to its right, then reverse the suffix after i) is applied 5 times in succession to [2, 4, 3, 1, 5]. What is the array afterwards?
- A. [2, 5, 3, 1, 4]
- B. [2, 4, 5, 3, 1]
- C. [2, 5, 1, 3, 4]
- D. [2, 5, 1, 4, 3]
Correct answer: D. [2, 5, 1, 4, 3]
The successive results are [2,4,3,5,1], [2,4,5,1,3], [2,4,5,3,1], [2,5,1,3,4] and finally [2,5,1,4,3]. Each result is the next arrangement in lexicographic order.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A max-heap is stored in A[1..13] = [100, 60, 90, 50, 40, 85, 80, 20, 10, 30, 35, 70, 75]. The key at position 8 (the value 20) is deleted…
- Binary search (lo = 0, hi = n − 1, mid = ⌊(lo + hi)/2⌋; each loop iteration compares a[mid] once and ends the search on equality) is run…
- Evaluate the postfix expression 8 2 3 ^ / 2 3 + 5 3 - - where ^ is exponentiation (x ^ y = xʸ), / is integer division and the operators…
- How many structurally distinct binary search trees can be built from 7 distinct keys?
- An AVL tree of height 6 (a single node has height 0) must contain at least how many nodes?
- Keys 12, 7, 19, 3, 25, 9, 14 are inserted one by one into an initially empty max-heap stored in an array (index 0 is the root), each…