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.

  1. A. 0 2 5 3 1 4
  2. B. 5 3 2 1 0 4
  3. C. 0 4 1 2 3 5
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic