GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int a[] = {5, 3, 8, 4, 2, 9, 1, 7, 6, 10}; int st[10], top = -1, nge[10]; for (int i = 0; i < 10; i++) nge[i] = -1; for (int i = 0; i < 10; i++) { while (top >= 0 && a[st[top]] < a[i]) nge[st[top--]] = a[i]; st[++top] = i; } What is the sum of nge[0..9], and how many elements remain on the stack at the end?
- A. 79 and 1
- B. 80 and 1
- C. 79 and 2
- D. 74 and 0
Correct answer: A. 79 and 1
nge = [8, 8, 9, 9, 9, 10, 7, 10, 10, -1] (the last stack elements keep -1), so the sum is 79; the stack ends holding only the final maximum.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A min-stack keeps an auxiliary stack: a value is pushed on it when it is less than or equal to the current auxiliary top (or it is empty)…
- for (i = 1; i < 1000; i = 2) for (j = 0; j < i; j++) cnt++; What is the final value of cnt (starting from 0)?
- A table of size 11 uses linear probing with h(k) = k mod 11 and tombstones: a deleted slot is marked DELETED; search continues past…
- The maximum of every window of 3 consecutive elements of [4, 2, 12, 11, -5, 7, 9, 3, 1, 6] is found with a deque of indices: before…
- A singly linked list has 25 nodes. A routine places pointer p at the head and pointer q at the 6th node from the head, then advances both…
- 12-bit sets are stored as bit vectors: A = 101101101101, B = 011010110011, C = 111000011010 (most significant bit first). What are the…