# Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space

Given an array arr[] of size n where every element is in range from 0 to n-1. Rearrange the given array so that arr[i] becomes arr[arr[i]]. This should be done with O(1) extra space.

Examples:

```Input: arr[]  = {3, 2, 0, 1}
Output: arr[] = {1, 0, 3, 2}
Explanation:
In the given array
arr[arr] is 1 so arr in output array is 1
arr[arr] is 0 so arr in output array is 0
arr[arr] is 3 so arr in output array is 3
arr[arr] is 2 so arr in output array is 2

Input: arr[] = {4, 0, 2, 1, 3}
Output: arr[] = {3, 4, 2, 0, 1}
Explanation:
arr[arr] is 3 so arr in output array is 3
arr[arr] is 4 so arr in output array is 4
arr[arr] is 2 so arr in output array is 2
arr[arr] is 0 so arr in output array is 0
arr[arr] is 1 so arr in output array is 1

Input: arr[] = {0, 1, 2, 3}
Output: arr[] = {0, 1, 2, 3}
Explanation:
arr[arr] is 0 so arr in output array is 0
arr[arr] is 1 so arr in output array is 1
arr[arr] is 2 so arr in output array is 2
arr[arr] is 3 so arr in output array is 3

```

If the extra space condition is removed, the question becomes very easy. The main part of the question is to do it without extra space.

## We strongly recommend that you click here and practice it, before moving on to the solution.

The credit for following solution goes to Ganesh Ram Sundaram .

Approach: The array elements of the given array lies from 0 to n-1. Now an array element is needed that can store two different values at the same time. To achieve this increment every element at ith index is incremented by (arr[arr[i]] % n)*n.After the increment operation of first step, every element holds both old values and new values. Old value can be obtained by arr[i]%n and a new value can be obtained by arr[i]/n.

How this can be achieved?
Let’s assume an element is a and another element is b, both the elements are less than n. So if an element a is incremented by b*n. So the element becomes a + b*n so when a + b*n is divided by n then the value is b and a + b*n % n is a.

Algorithm:

1. Traverse the array from start to end.
2. For every index increment the element by array[array[index] % n]. To get the ith element find the modulo with n, i.e array[index]%n.
3. Again Travsese the array from start to end
4. Print the ith element after dividing the ith element by n, i.e. array[i]/n.

Implementation:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// The function to rearrange an array  ` `// in-place so that arr[i] becomes arr[arr[i]]. ` `void` `rearrange(``int` `arr[], ``int` `n) ` `{ ` `    ``// First step: Increase all values by (arr[arr[i]]%n)*n ` `    ``for` `(``int` `i=0; i < n; i++) ` `        ``arr[i] += (arr[arr[i]]%n)*n; ` ` `  `    ``// Second Step: Divide all values by n ` `    ``for` `(``int` `i=0; i

## Java

 `class` `Rearrange  ` `{ ` `    ``// The function to rearrange an array in-place so that arr[i] ` `    ``// becomes arr[arr[i]]. ` `    ``void` `rearrange(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``// First step: Increase all values by (arr[arr[i]]%n)*n ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``arr[i] += (arr[arr[i]] % n) * n; ` ` `  `        ``// Second Step: Divide all values by n ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``arr[i] /= n; ` `    ``} ` ` `  `    ``// A utility function to print an array of size n ` `    ``void` `printArr(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `        ``System.out.println(``""``); ` `    ``} ` ` `  `    ``/* Driver program to test above functions */` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``Rearrange rearrange = ``new` `Rearrange(); ` `        ``int` `arr[] = {``3``, ``2``, ``0``, ``1``}; ` `        ``int` `n = arr.length; ` ` `  `        ``System.out.println(``"Given Array is :"``); ` `        ``rearrange.printArr(arr, n); ` ` `  `        ``rearrange.rearrange(arr, n); ` ` `  `        ``System.out.println(``"Modified Array is :"``); ` `        ``rearrange.printArr(arr, n); ` `    ``} ` `} ` ` `  `// This code has been contributed by Mayank Jaiswal `

## Python3

 `# Python3 program to Rearrange  ` `# an array so that arr[i] becomes  ` `# arr[arr[i]] ` ` `  `# The function to rearrange an ` `# array in-place so that arr[i] ` `# becomes arr[arr[i]]. ` `def` `rearrange(arr, n): ` ` `  `    ``# First step: Increase all values ` `    ``# by (arr[arr[i]] % n) * n ` `    ``for` `i ``in` `range``(``0``, n): ` `        ``arr[i] ``+``=` `(arr[arr[i]] ``%` `n) ``*` `n ` ` `  `    ``# Second Step: Divide all values ` `    ``# by n ` `    ``for` `i ``in` `range``(``0``, n): ` `        ``arr[i] ``=` `int``(arr[i] ``/` `n) ` ` `  `# A utility function to print  ` `# an array of size n ` `def` `printArr(arr, n): ` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``print` `(arr[i], end ``=``" "``) ` `    ``print` `("") ` ` `  `# Driver program ` `arr ``=` `[``3``, ``2``, ``0``, ``1``] ` `n ``=` `len``(arr) ` ` `  `print` `(``"Given array is"``) ` `printArr(arr, n) ` ` `  `rearrange(arr, n); ` `print` `(``"Modified array is"``) ` `printArr(arr, n) ` ` `  `# This code is contributed by shreyanshi_arun `

## C#

 `// C# Program to rearrange an array ` `// so that arr[i] becomes arr[arr[i]]  ` `// with O(1) extra space ` `using` `System; ` ` `  `class` `Rearrange ` `{ ` `     `  `    ``// Function to rearrange an  ` `    ``// array in-place so that arr[i] ` `    ``// becomes arr[arr[i]]. ` `    ``void` `rearrange(``int` `[]arr, ``int` `n)  ` `    ``{ ` `         `  `        ``// First step: Increase all values  ` `        ``// by (arr[arr[i]] % n) * n ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``arr[i] += (arr[arr[i]] % n) * n; ` ` `  `        ``// Second Step: Divide all values by n ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``arr[i] /= n; ` `    ``} ` ` `  `    ``// A utility function to  ` `    ``// print an array of size n ` `    ``void` `printArr(``int` `[]arr, ``int` `n)  ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``Console.WriteLine(``""``); ` `    ``} ` ` `  `    ``//  Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``Rearrange rearrange = ``new` `Rearrange(); ` `        ``int` `[]arr = {3, 2, 0, 1}; ` `        ``int` `n = arr.Length; ` ` `  `        ``Console.Write(``"Given Array is :"``); ` `        ``rearrange.printArr(arr, n); ` ` `  `        ``rearrange.rearrange(arr, n); ` ` `  `        ``Console.Write(``"Modified Array is :"``); ` `        ``rearrange.printArr(arr, n); ` `    ``} ` `} ` ` `  `// This code has been contributed by Nitin Mittal. `

## PHP

 ` `

Output:

```Given array is
3 2 0 1
Modified array is
1 0 3 2```

Complexity Analysis:

• Time Complexity: O(n), Only one traversal of the array is needed. So time complexity is O(n).
• Auxiliary Space: O(1), No extra space is required.

The problem with the above solution is, it may cause an overflow.

Here is a better solution:
Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’

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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

49

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.