Data Structures & AlgorithmsHardTCS Digital · Infosys SP
Counting sort on [1, 1, 2, 1, 0, 3, 3, 0, 2, 0] (values 0–4) builds count[], turns it into cumulative counts, then scans the input from RIGHT to LEFT placing each value v at index count[v] − 1 (0-based) and decrementing count[v]. What is the cumulative count array, and at which index is the LAST input element (0) placed?
- A. [3, 6, 8, 10, 10]; index 2
- B. [3, 6, 8, 10, 10]; index 3
- C. [3, 6, 8, 10, 10]; index 0
- D. [3, 3, 2, 2, 0]; index 2
Correct answer: A. [3, 6, 8, 10, 10]; index 2
Cumulative counts [3, 6, 8, 10, 10] give the end of each value's block. Scanning from the right places the last 0 at the last slot of its block, index 2, which keeps the sort stable.
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
- What is the length of the shortest contiguous subarray of [1, 3, 1, 8, 7, 3, 4, 7, 1, 2] whose sum is at least 20? (All values are…
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [0…
- Rotate a square matrix 90° clockwise in place by transposing and then reversing each row. What is printed? python m = [[1, 2, 3], [4, 5…
- For the binary array [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the…
- Which application does NOT naturally use a stack?
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…