Data Structures & AlgorithmsHardGATE CS · ICPC regional
How many index pairs i < j satisfy a[i] - i = a[j] - j for the array [7, 3, 5, 8, 6, 9, 4, 10] (0-based indices)?
- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
Rewriting the condition as a[i] - i = a[j] - j turns it into counting equal keys in a hash map of shifted values, which runs in O(n).
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
- How many inversions (pairs i < j with a[i] > a[j]) does the array [3, 1, 4, 1, 5, 9, 2, 6] contain?
- How many index triples i < j < k in the array [1, 2, 3, 4, 5, 6] have a[i] + a[j] + a[k] divisible by 5?
- A queue has capacity 4 and an enqueue into a full queue is rejected. Operations in order: enqueue 1, 2, 3, 4; dequeue; dequeue; enqueue 5…
- What is the sum of the maxima of every contiguous window of size 3 in the array [1, 3, -1, -3, 5, 3, 6, 7]?
- How many valid sequences of 4 push and 4 pop operations are there on an initially empty stack, where every pop is applied only to a…
- Three processes arrive at time 0 with CPU bursts 5, 3 and 8. Round-robin scheduling uses a time quantum of 2, a process that is not…