Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: Find the number of pairs of indices (i, j), i < j, in the array [1, 3, 2, 4, 3, 5] such that a[i] = a[j] + 1 or a[i] = a[j] − 1.
- A. 2
- B. 6
- C. 7
- D. 4
Correct answer: B. 6
Pairs with |a[i] − a[j]| = 1 over all i < j number 6. Checking only adjacent positions gives 2, allowing equal values (|diff| ≤ 1) gives 7, and testing only a[j] = a[i] + 1 gives 4.
More Coding Interview Problems questions
- Problem: A grid has 3 rows and 4 columns. Starting at (0,0), a move goes right or down, and cells with odd row+column are forbidden. How…
- Problem: For the words ["bella", "label", "roller", "dinner", "ladder"], how many pairs (i, j) with i < j are anagrams of each other (same…
- Problem: The number 2024 is factorised. How many positive integers less than 10000 are divisors of 2024 × 2025?
- Problem: How many subarrays of [1, 2, 3, 1, 2, 3, 4] contain exactly 3 distinct values? Run the brute-force code. python a = [1, 2, 3, 1…
- Problem: How many integers in [1, 1000] are divisible by 7, 11 or 13? Run the code. python print(sum(1 for x in range(1, 1001) if x % 7 ==…
- Problem: What is the number of trailing zeros in 2025! (in decimal)? Run the code. python n = 2025 c = 0 p = 5 while p <= n: c += n // p p…