Data Structures & AlgorithmsHardGATE CS · DSA
Two pointers start at both ends of the sorted array [2, 5, 10, 11, 12, 14, 16, 18, 21, 29]. How many pairs (i < j) have a[i] + a[j] ≤ 28? (When a[lo] + a[hi] ≤ target, all pairs (lo, lo+1..hi) qualify.)
- A. 26
- B. 25
- C. 2
- D. 23
Correct answer: B. 25
If a[lo] + a[hi] ≤ 28, every j in (lo, hi] also works with lo, so add hi − lo and advance lo; otherwise decrement hi. The total is 25 (of which 2 pairs sum to exactly 28).
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
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…
- For an n × n matrix whose rows and columns are both sorted ascending, what is the time of the staircase search for a key?
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Partition a linked list around x: all nodes with value < x come first, then the rest, each part keeping its original relative order. What…