VOOZH about

URL: https://www.geeksforgeeks.org/dsa/reorder-a-array-according-to-given-indexes/

⇱ Reorder an array according to given indexes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reorder an array according to given indexes

Last Updated : 23 Jul, 2025

Given two integer arrays of the same length, arr[] and index[], the task is to reorder the elements in arr[] such that after reordering, each element from arr[i] moves to the position index[i]. The new arrangement reflects the values being placed at their target indices, as described by index[] array.

Example:

Input: arr[] = [10, 11, 12], index[] = [1, 0, 2]
Output: 11 10 12
Explanation: 10 moves to position 1, 11 to 0, and 12 stays at 2.

Input: arr[] = [1, 2, 3, 4], index[] = [3, 2, 0, 1]
Output: 3 4 2 1
Explanation: 1 moves to 3, 2 to 2, 3 to 0, 4 to 1.

Input: arr[] = [50, 40, 70, 60, 90], index[] = [3,  0,  4,  1,  2]
Output: 40 60 90 50 70

[Naive Approach] Using Sorting - O(n*log(n)) Time and O(n) Space

The idea is to pair each element in arr[] with its target position from index[] as a 2D array. These pairs are then sorted by index, which arranges elements in the order they should appear in the final array. After sorting, we extract only the values (second part of each pair) to build the reordered array.


Output
11 10 12 

[Better Approach] Using an Auxiliary Array - O(n) Time and O(n) Space

The idea is to reorder using an auxiliary array to temporarily hold the reordered elements by placing each element at the target index. Afterward, the reordered values are copied back into the original arr[].


Output
11 10 12 

[Expected Approach - 1] Using Cyclic Sort - O(n) Time and O(1) Space

The idea is use cyclic sort technique to reorder elements in the arr[] array based on the specified index[]. It iterates through the elements of arr[] and, for each element, continuously swaps it with the element at its correct position according to index[]. The process continues until each element is at its intended position, ensuring the desired order is achieved.

Steps to implement the above idea:

  • Start a while loop with variable i = 0 and continue until i < length(arr), to process every element.
  • For each element, check if it's already in the correct position using index[i] == i to avoid unnecessary swaps.
  • If it is, just increment i to proceed to the next position, as the current one is already placed correctly.
  • If not, perform a manual swap of arr[i] and arr[index[i]] using a temporary variable to avoid data loss.
  • Alongside, manually swap index[i] with index[index[i]] to maintain synchronization between the index and value arrays.
  • Repeat this process until all elements are at their correct index according to the index[] array, ensuring correctness.
  • Once the loop completes, print the final reordered arr[] which now matches the intended layout defined by index[].

Output
11 10 12 

[Expected Approach - 2] Using Mathematics - O(n) Time and O(1) Space

The idea is to reorder elements in-place without using extra space by encoding two numbers (original and new) into a single number. Since each element in arr[] is less than or equal to the maximum value, we pick value = max + 1 to ensure uniqueness when combining.

We update arr[index[i]] using -> arr[index[i]] += (arr[i] % value) * value, which embeds both current and new values in the same cell.

  • The % value ensures we use the original value even after updates.
  • The * value shifts the new value to a higher place (like storing digits).

In the final step, dividing every element by value removes the original part and leaves only the reordered value


Output
11 10 12 
Comment
Article Tags:
Article Tags: