Data Structures & AlgorithmsModerateTCS Digital · Infosys SP
How many inversions (pairs i < j with a[i] > a[j]) does [35, 33, 31, 3, 20, 18, 13, 4] have? (This is also the number of swaps bubble sort makes.)
- A. 24
- B. 25
- C. 6
- D. 23
Correct answer: A. 24
Count, for each element, the smaller elements to its right; the total is 24. Counting only adjacent out-of-order pairs gives 6.
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
- The Dutch national flag algorithm (pointers low, mid, high; 0 → swap with low and advance both; 1 → advance mid; 2 → swap with high and…
- A mountain is a contiguous subarray of length ≥ 3 that strictly increases to a single peak and then strictly decreases. What is the length…
- For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed? python…
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…
- An array holds n distinct elements. What is the minimum number of comparisons that ANY comparison-based algorithm needs, in the worst…
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [35, 8…