Given two integer arrays of same size, “arr[]” and “index[]”, reorder elements in “arr[]” according to given index array. It is not allowed to given array arr’s length.
Example:
Input: arr[] = [10, 11, 12];
index[] = [1, 0, 2];
Output: arr[] = [11, 10, 12]
index[] = [0, 1, 2]
Input: arr[] = [50, 40, 70, 60, 90]
index[] = [3, 0, 4, 1, 2]
Output: arr[] = [40, 60, 90, 50, 70]
index[] = [0, 1, 2, 3, 4]
Expected time complexity O(n) and auxiliary space O(1)
We strongly recommend you to minimize your browser and try this yourself first.
A Simple Solution is to use an auxiliary array temp[] of same size as given arrays. Traverse the given array and put all elements at their correct place in temp[] using index[]. Finally copy temp[] to arr[] and set all values of index[i] as i.
C++
#include<iostream>
using namespace std;
void reorder( int arr[], int index[], int n)
{
int temp[n];
for ( int i=0; i<n; i++)
temp[index[i]] = arr[i];
for ( int i=0; i<n; i++)
{
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
int arr[] = {50, 40, 70, 60, 90};
int index[] = {3, 0, 4, 1, 2};
int n = sizeof (arr)/ sizeof (arr[0]);
reorder(arr, index, n);
cout << "Reordered array is: \n" ;
for ( int i=0; i<n; i++)
cout << arr[i] << " " ;
cout << "\nModified Index array is: \n" ;
for ( int i=0; i<n; i++)
cout << index[i] << " " ;
return 0;
}
|
Java
import java.util.Arrays;
class Test
{
static int arr[] = new int []{ 50 , 40 , 70 , 60 , 90 };
static int index[] = new int []{ 3 , 0 , 4 , 1 , 2 };
static void reorder()
{
int temp[] = new int [arr.length];
for ( int i= 0 ; i<arr.length; i++)
temp[index[i]] = arr[i];
for ( int i= 0 ; i<arr.length; i++)
{
arr[i] = temp[i];
index[i] = i;
}
}
public static void main(String[] args)
{
reorder();
System.out.println( "Reordered array is: " );
System.out.println(Arrays.toString(arr));
System.out.println( "Modified Index array is:" );
System.out.println(Arrays.toString(index));
}
}
|
Python3
def reorder(arr,index, n):
temp = [ 0 ] * n;
for i in range ( 0 ,n):
temp[index[i]] = arr[i]
for i in range ( 0 ,n):
arr[i] = temp[i]
index[i] = i
arr = [ 50 , 40 , 70 , 60 , 90 ]
index = [ 3 , 0 , 4 , 1 , 2 ]
n = len (arr)
reorder(arr, index, n)
print ( "Reordered array is:" )
for i in range ( 0 ,n):
print (arr[i],end = " " )
print ( "\nModified Index array is:" )
for i in range ( 0 ,n):
print (index[i],end = " " )
|
C#
using System;
public class Test{
static int []arr = new int []{50, 40, 70, 60, 90};
static int []index = new int []{3, 0, 4, 1, 2};
static void reorder()
{
int []temp = new int [arr.Length];
for ( int i=0; i<arr.Length; i++)
temp[index[i]] = arr[i];
for ( int i=0; i<arr.Length; i++)
{
arr[i] = temp[i];
index[i] = i;
}
}
public static void Main()
{
reorder();
Console.WriteLine( "Reordered array is: " );
Console.WriteLine( string .Join( "," , arr));
Console.WriteLine( "Modified Index array is:" );
Console.WriteLine( string .Join( "," , index));
}
}
|
PHP
<?php
function reorder( $arr , $index , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$temp [ $index [ $i ]] = $arr [ $i ];
}
for ( $i = 0; $i < $n ; $i ++)
{
$arr [ $i ] = $temp [ $i ];
$index [ $i ] = $i ;
}
echo "Reordered array is: \n" ;
for ( $i = 0; $i < $n ; $i ++)
{
echo $arr [ $i ] . " " ;
}
echo "\nModified Index array is: \n" ;
for ( $i = 0; $i < $n ; $i ++)
{
echo $index [ $i ] . " " ;
}
}
$arr = array (50, 40, 70, 60, 90);
$index = array (3, 0, 4, 1, 2);
$n = sizeof( $arr );
reorder( $arr , $index , $n );
?>
|
Javascript
<script>
function reorder(arr, index, n) {
var temp = [...Array(n)];
for ( var i = 0; i < n; i++) temp[index[i]] = arr[i];
for ( var i = 0; i < n; i++) {
arr[i] = temp[i];
index[i] = i;
}
}
var arr = [50, 40, 70, 60, 90];
var index = [3, 0, 4, 1, 2];
var n = arr.length;
reorder(arr, index, n);
document.write( "Reordered array is: " );
document.write( "<br>" );
for ( var i = 0; i < n; i++) document.write(arr[i] + " " );
document.write( "<br>" );
document.write( "Modified Index array is: " );
document.write( "<br>" );
for ( var i = 0; i < n; i++) document.write(index[i] + " " );
</script>
|
Output:
Reordered array is:
40 60 90 50 70
Modified Index array is:
0 1 2 3 4
Thanks to gccode for suggesting above solution.
We can solve it Without Auxiliary Array. Below is algorithm.
1) Do following for every element arr[i]
a) While index[i] is not equal to i
(i) Store array and index values of the target (or
correct) position where arr[i] should be placed.
The correct position for arr[i] is index[i]
(ii) Place arr[i] at its correct position. Also
update index value of correct position.
(iii) Copy old values of correct position (Stored in
step (i)) to arr[i] and index[i] as the while
loop continues for i.
Below is implementation of above algorithm.
C++
#include<iostream>
using namespace std;
void reorder( int arr[], int index[], int n)
{
for ( int i=0; i<n; i++)
{
while (index[i] != i)
{
int oldTargetI = index[index[i]];
char oldTargetE = arr[index[i]];
arr[index[i]] = arr[i];
index[index[i]] = index[i];
index[i] = oldTargetI;
arr[i] = oldTargetE;
}
}
}
int main()
{
int arr[] = {50, 40, 70, 60, 90};
int index[] = {3, 0, 4, 1, 2};
int n = sizeof (arr)/ sizeof (arr[0]);
reorder(arr, index, n);
cout << "Reordered array is: \n" ;
for ( int i=0; i<n; i++)
cout << arr[i] << " " ;
cout << "\nModified Index array is: \n" ;
for ( int i=0; i<n; i++)
cout << index[i] << " " ;
return 0;
}
|
Java
import java.util.Arrays;
class Test
{
static int arr[] = new int []{ 50 , 40 , 70 , 60 , 90 };
static int index[] = new int []{ 3 , 0 , 4 , 1 , 2 };
static void reorder()
{
for ( int i= 0 ; i<arr.length; i++)
{
while (index[i] != i)
{
int oldTargetI = index[index[i]];
char oldTargetE = ( char )arr[index[i]];
arr[index[i]] = arr[i];
index[index[i]] = index[i];
index[i] = oldTargetI;
arr[i] = oldTargetE;
}
}
}
public static void main(String[] args)
{
reorder();
System.out.println( "Reordered array is: " );
System.out.println(Arrays.toString(arr));
System.out.println( "Modified Index array is:" );
System.out.println(Arrays.toString(index));
}
}
|
Python3
def reorder(arr, index, n):
for i in range ( 0 ,n):
while (index[i] ! = i):
oldTargetI = index[index[i]]
oldTargetE = arr[index[i]]
arr[index[i]] = arr[i]
index[index[i]] = index[i]
index[i] = oldTargetI
arr[i] = oldTargetE
arr = [ 50 , 40 , 70 , 60 , 90 ]
index = [ 3 , 0 , 4 , 1 , 2 ]
n = len (arr)
reorder(arr, index, n)
print ( "Reordered array is:" )
for i in range ( 0 , n):
print (arr[i],end = " " )
print ( "\nModified Index array is:" )
for i in range ( 0 , n):
print (index[i] ,end = " " )
|
C#
using System;
public class Test
{
static int []arr = new int []{50, 40, 70, 60, 90};
static int []index = new int []{3, 0, 4, 1, 2};
static void reorder()
{
for ( int i=0; i<arr.Length; i++)
{
while (index[i] != i)
{
int oldTargetI = index[index[i]];
char oldTargetE = ( char )arr[index[i]];
arr[index[i]] = arr[i];
index[index[i]] = index[i];
index[i] = oldTargetI;
arr[i] = oldTargetE;
}
}
}
public static void Main()
{
reorder();
Console.WriteLine( "Reordered array is: " );
Console.WriteLine(String.Join( " " ,arr));
Console.WriteLine( "Modified Index array is:" );
Console.WriteLine(String.Join( " " ,index));
}
}
|
Output:
Reordered array is:
40 60 90 50 70
Modified Index array is:
0 1 2 3 4
Thanks to shyamala_lokre for suggesting above solution.
Another Method without using an auxiliary array is to sort the arrays.
Sort the index array and customize the sort to swap the arr[] data whenever you swap the index[] data.
C++
#include <bits/stdc++.h>
using namespace std;
int heapSize;
void swap ( int &a, int &b ) {
int temp = a;
a = b;
b = temp;
}
void heapify( int arr[], int index[], int i )
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if ( left < heapSize && index[left] > index[largest] )
{
largest = left;
}
if ( right < heapSize && index[right] > index[largest] )
{
largest = right;
}
if ( largest != i ) {
swap(arr[largest], arr[i]);
swap(index[largest], index[i]);
heapify(arr, index, largest);
}
}
void heapSort( int arr[], int index[], int n ) {
for ( int i = ( n - 1 ) / 2 ; i >= 0 ; i-- ) {
heapify(arr, index, i);
}
for ( int i = n - 1 ; i > 0 ; i-- ) {
swap(index[0], index[i]);
swap(arr[0], arr[i]);
heapSize--;
heapify(arr, index, 0);
}
}
int main() {
int arr[] = {50, 40, 70, 60, 90};
int index[] = {3, 0, 4, 1, 2};
int n = sizeof (arr)/ sizeof (arr[0]);
heapSize = n;
heapSort(arr, index, n);
cout << "Reordered array is: \n" ;
for ( int i = 0 ; i < n ; i++ )
cout << arr[i] << " " ;
cout << "\nModified Index array is: \n" ;
for ( int i=0; i<n; i++)
cout << index[i] << " " ;
return 0;
}
|
Java
class GFG{
static int heapSize;
public static void heapify( int arr[],
int index[], int i)
{
int largest = i;
int left = 2 * i + 1 ;
int right = 2 * i + 2 ;
if (left < heapSize &&
index[left] > index[largest] )
{
largest = left;
}
if (right < heapSize &&
index[right] > index[largest] )
{
largest = right;
}
if (largest != i)
{
int temp = arr[largest];
arr[largest] = arr[i];
arr[i] = temp;
temp = index[largest];
index[largest] = index[i];
index[i] = temp;
heapify(arr, index, largest);
}
}
public static void heapSort( int arr[],
int index[], int n)
{
for ( int i = (n - 1 ) / 2 ; i >= 0 ; i--)
{
heapify(arr, index, i);
}
for ( int i = n - 1 ; i > 0 ; i--)
{
int temp = index[ 0 ];
index[ 0 ] = index[i];
index[i] = temp;
temp = arr[ 0 ];
arr[ 0 ] = arr[i];
arr[i] = temp;
heapSize--;
heapify(arr, index, 0 );
}
}
public static void main(String[] args)
{
int arr[] = { 50 , 40 , 70 , 60 , 90 };
int index[] = { 3 , 0 , 4 , 1 , 2 };
int n = arr.length;
heapSize = n;
heapSort(arr, index, n);
System.out.println( "Reordered array is: " );
for ( int i = 0 ; i < n ; i++)
System.out.print(arr[i] + " " );
System.out.println();
System.out.println( "Modified Index array is: " );
for ( int i = 0 ; i < n; i++)
System.out.print(index[i] + " " );
}
}
|
Python3
def heapify(arr, index, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
global heapSize
if (left < heapSize and
index[left] > index[largest]):
largest = left
if (right < heapSize and
index[right] > index[largest]):
largest = right
if (largest ! = i):
arr[largest], arr[i] = arr[i], arr[largest]
index[largest], index[i] = index[i], index[largest]
heapify(arr, index, largest)
def heapSort(arr, index, n):
global heapSize
for i in range ( int ((n - 1 ) / 2 ), - 1 , - 1 ):
heapify(arr, index, i)
for i in range (n - 1 , 0 , - 1 ):
index[ 0 ], index[i] = index[i], index[ 0 ]
arr[ 0 ], arr[i] = arr[i], arr[ 0 ]
heapSize - = 1
heapify(arr, index, 0 )
arr = [ 50 , 40 , 70 , 60 , 90 ]
index = [ 3 , 0 , 4 , 1 , 2 ]
n = len (arr)
global heapSize
heapSize = n
heapSort(arr, index, n)
print ( "Reordered array is: " )
print ( * arr, sep = ' ' )
print ( "Modified Index array is: " )
print ( * index, sep = ' ' )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int heapSize;
public static void heapify( int [] arr,
int [] index,
int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < heapSize &&
index[left] > index[largest] )
{
largest = left;
}
if (right < heapSize &&
index[right] > index[largest] )
{
largest = right;
}
if (largest != i)
{
int temp = arr[largest];
arr[largest] = arr[i];
arr[i] = temp;
temp = index[largest];
index[largest] = index[i];
index[i] = temp;
heapify(arr, index, largest);
}
}
public static void heapSort( int [] arr,
int [] index,
int n)
{
for ( int i = (n - 1) / 2 ; i >= 0 ; i--)
{
heapify(arr, index, i);
}
for ( int i = n - 1 ; i > 0 ; i--)
{
int temp = index[0];
index[0] = index[i];
index[i] = temp;
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapSize--;
heapify(arr, index, 0);
}
}
static void Main()
{
int [] arr = { 50, 40, 70, 60, 90 };
int [] index = { 3, 0, 4, 1, 2 };
int n = arr.Length;
heapSize = n;
heapSort(arr, index, n);
Console.WriteLine( "Reordered array is: " );
for ( int i = 0 ; i < n ; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
Console.WriteLine( "Modified Index array is: " );
for ( int i = 0; i < n; i++)
Console.Write(index[i] + " " );
}
}
|
Output:
Reordered array is:
40 60 90 50 70
Modified Index array is:
0 1 2 3 4
Time Complexity: O(nlogn)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.