GATE Computer ScienceVery hardGATE CS (PYQ pattern)
How many pairs of positions i < j in the sorted array [1, 3, 4, 6, 7, 9, 11, 14, 15, 19] have a[i] + a[j] ≤ 20?
- A. 27
- B. 29
- C. 31
- D. 33
Correct answer: B. 29
Two pointers: for each i, the largest j with a[i] + a[j] ≤ 20 counts all partners between them: 9, 7, 6, 4, 2, 1, 0… summing the per-element counts gives 29 pairs in O(n) time.
More Algorithms questions
- What is the minimum weight of a spanning tree that must contain the edge 3-4 (weight 15) in the graph with edges 0-1 (7), 0-3 (5), 1-2…
- Lazy-deletion Dijkstra (a new heap entry is pushed whenever a label improves; stale entries are skipped when popped) runs from A on the…
- A directed graph has edges 0→1 (3), 1→2 (2), 2→0 (4), 1→3 (1), 3→4 (5), 4→1 (−1), 2→4 (6), 4→0 (2), 3→0 (1). What is the minimum mean…
- How many spanning trees does K₅ minus one edge have (5 vertices, all pairs joined except one)?
- How many times does x++ execute? for (i = 1; i < 200; i = 2) for (j = i; j < 200; j += i) x++;
- The solution of the recurrence T(n) = 2T(n/2) + n/log n (with T(1) = 1, logs base 2) is: