Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: How many pairs (i, j) with i < j in the array [4, 8, 1, 5, 9, 2, 7, 3, 6] satisfy a[j] − a[i] = 3?
- A. 3
- B. 1
- C. 4
- D. 2
Correct answer: D. 2
Scanning pairs with i < j and testing a[j] − a[i] = 3 counts each index pair once. Testing a[i] − a[j] or ignoring the index order double-counts or misses pairs.
More Coding Interview Problems questions
- Problem: How many contiguous subarrays of [7, 2, 9, 4, 1, 8, 3, 5, 6, 10] have a sum divisible by 7?
- Problem: Floyd–Warshall is run on 4 vertices with directed edges 0→1 (3), 1→2 (4), 0→2 (9), 2→3 (1), 3→0 (2), 1→3 (5). Unreachable pairs…
- Problem: On a 6×6 grid, a robot starts at the top-left corner and moves only right or down to the bottom-right corner. The cells (1,2)…
- Problem: Place 4 non-attacking rooks on a 4×4 board (no two share a row or column) such that none is on a forbidden cell. Forbidden cells…
- Problem: In a grid of 0s and 1s, a connected region is a group of 1-cells that are connected when moving to 8 neighbours (up, down, left…
- Problem: A rooted tree is given by parent array p = [-1, 0, 0, 1, 1, 2, 5, 5, 3] for nodes 0–8 (node 0 is the root). How many nodes are at…