Data Structures & AlgorithmsModerateGATE CS · TCS NQT
How many index pairs i < j in the array [1, 5, 3, 4, 2, 7, 9] have absolute difference exactly 2?
- A. 6
- B. 4
- C. 7
- D. 5
Correct answer: D. 5
For each value x, check whether x+2 and x-2 appear; counting both directions double-counts the same pair.
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
- In the array [1, 5, 5, 5, 5, 5, 5, 5, 5, 9], how many distinct value pairs {x, y} with x <= y satisfy x + y = 10, where x and y both occur…
- What is the length of the longest substring of aabccbb that can be made into a string of one repeated letter by replacing at most 2…
- How many substrings of abcabc contain all of the three letters a, b and c at least once?
- How many substrings of aabacbebebe contain exactly two distinct characters?
- How many substrings of abcdabcabd are anagrams of the pattern abd (same letters with the same multiplicities)?
- How many contiguous subarrays of [1, 2, 2, 3, 1, 1] contain no two adjacent equal elements?