Data Structures & AlgorithmsVery hard
Stably sort the positions of -3 2 -2 1 3 -1 by the ABSOLUTE value of the element at each position (ascending), keeping the input order for equal absolute values. Give the ORIGINAL 0-based indices in the order they appear after sorting.
- A. 0 2 5 3 1 4
- B. 5 3 2 1 0 4
- C. 0 4 1 2 3 5
- D. 3 5 1 2 0 4
Correct answer: D. 3 5 1 2 0 4
Python's sort with key=abs is stable, so equal absolute values keep their input order: index 3 (value 1) before index 5 (value −1), then indices 1 and 2, and so on. Sorting by signed value, breaking ties by value, or reversing the order each change the index sequence.
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
- How many index pairs i < j in 1 3 2 5 4 6 8 satisfy A[j] − A[i] = j − i?
- For 3 1 4 1 5 9 2 6, find the smallest 0-based index t such that the sum of the elements A[0..t] is MORE than half of the total sum of the…
- A binary array of length 10^6 needs its longest contiguous subarray with equally many 0s and 1s. Which approach is correct and linear?
- For an array of 10^5 integers you must count index pairs with a[i] XOR a[j] equal to a fixed K. Which method is the best fit?
- How many contiguous subarrays of [2, 4, 6, 8] have every element divisible by 4?
- What is the length of the longest contiguous subarray of [3, 1, 4, 1, 5, 9, 2, 6] whose sum is divisible by 3?