Data Structures & AlgorithmsModerateTCS NQT · Infosys
Array [4, 4, 7, 1, 7, 2]. How many index pairs i < j have equal values a[i] = a[j]?
- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
Each value that appears c times contributes C(c, 2) equal pairs. The value 4 appears twice and 7 appears twice, so there are 1 + 1 = 2 pairs; counting distinct values is a different quantity.
More Coding Interview Problems questions
- Array [3, 7, 2, 9, 4]. How many contiguous subarrays sum to exactly 9?
- Problem: Fresh oranges (1) rot their neighbours (4-directional) one minute at a time; 0 is empty and 2 is rotten. How many minutes until…
- Problem: How many distinct palindromic substrings does 'aaaa' have? Run the code. python s = 'aaaa' print(len({s[i:j] for i in…
- Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code…
- Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code…
- Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code…