This post is completed by 1 user

  • 0
Add to List
Medium

486. Sort the indexes of the array as per the elements of the array

Objective: Given an array, write a java program to sort the indexes of the array according to the elements of the given array ( do not sort the given array)

Example:

Input Array: [5, 6, 1, 2, 8, 4, 3, 0]
Sorted indices as per input array: [7, 2, 3, 6, 5, 0, 1, 4]

Input Array: [4, 3, 2, 1]
Sorted indices as per input array: [3, 2, 1, 0]

Input Array: [1, 2, 3, 4]
Sorted indices as per input array: [0, 1, 2, 3]

Approach:

Create an array of indices and store 0 to n-1 (n is the size of the given array). Now sort the array by overriding the Comparator in which you will compare the elements of the given array for indices array.

Output:

Input Array: [5, 6, 1, 2, 8, 4, 3, 0]
Sorted indices as per input array: [7, 2, 3, 6, 5, 0, 1, 4]