Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
Last Updated :
24 Apr, 2025
Given an array arr[] of size n where every element is in the 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: arr[arr[0]] is 1 so arr[0] in output array is 1
arr[arr[1]] is 0 so arr[1] in output array is 0
arr[arr[2]] is 3 so arr[2] in output array is 3
arr[arr[3]] is 2 so arr[3] in output array is 2
Input: arr[] = [4, 0, 2, 1, 3]
Output: arr[] = [3, 4, 2, 0, 1]
Explanation: arr[arr[0]] is 3 so arr[0] in output array is 3
arr[arr[1]] is 4 so arr[1] in output array is 4
arr[arr[2]] is 2 so arr[2] in output array is 2
arr[arr[3]] is 0 so arr[3] in output array is 0
arr[arr[4]] is 1 so arr[4] in output array is 1
Input: arr[] = [0, 1, 2, 3]
Output: arr[] = [0, 1, 2, 3]
Explanation: arr[arr[0]] is 0 so arr[0] in output array is 0
arr[arr[1]] is 1 so arr[1] in output array is 1
arr[arr[2]] is 2 so arr[2] in output array is 2
arr[arr[3]] is 3 so arr[3] in output array is 3
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.
The array elements of the given array lie from 0 to n-1. Now an array element is needed that can store two different values at the same time. To achieve this, every element at ith index is incremented by (arr[arr[i]] % n)*n. After the increment operation of the first step, every element holds both old values and new values. An old value can be obtained by arr[i]%n and a new value can be obtained by arr[i]/n.
Note: This Approach may cause an overflow.
Algorithm:
- Traverse the array from start to end.
- 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.
- Again Traverse the array from start to end
- Print the ith element after dividing the ith element by n, i.e. array[i]/n.
C++
#include <iostream>
#include <vector>
using namespace std;
void rearrange(vector<int>arr)
{
int n=arr.size();
// 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;
}
void printArr(vector<int>arr)
{
int n=arr.size();
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
vector<int>arr = { 3, 2, 0, 1 };
rearrange(arr);
printArr(arr);
return 0;
}
Java
import java.util.*;
class GfG{
static void rearrange(int[] arr) {
int n = arr.length;
// 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;
}
static void printArr(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String[] args) {
int[] arr = { 3, 2, 0, 1 };
rearrange(arr);
printArr(arr);
}
}
Python
def rearrange(arr):
n = len(arr)
# First step: Increase all values
# by (arr[arr[i]]%n)*n
for i in range(n):
arr[i] += (arr[arr[i]] % n) * n
# Second Step: Divide all values by n
for i in range(n):
arr[i] //= n
def printArr(arr):
n = len(arr)
for i in range(n):
print(arr[i], end=' ')
print()
if __name__ == '__main__':
arr = [3, 2, 0, 1]
rearrange(arr)
printArr(arr)
C#
using System;
using System.Collections.Generic;
class GfG{
static void Rearrange(int[] arr) {
int n = arr.Length;
// 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;
}
static void PrintArr(int[] arr) {
int n = arr.Length;
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
static void Main() {
int[] arr = { 3, 2, 0, 1 };
Rearrange(arr);
PrintArr(arr);
}
}
JavaScript
function rearrange(arr) {
const n = arr.length;
// First step: Increase all values
// by (arr[arr[i]]%n)*n
for (let i = 0; i < n; i++)
arr[i] += (arr[arr[i]] % n) * n;
// Second Step: Divide all values by n
for (let i = 0; i < n; i++)
arr[i] = Math.floor(arr[i] / n);
}
function printArr(arr) {
const n = arr.length;
for (let i = 0; i < n; i++)
process.stdout.write(arr[i] + ' ');
console.log();
}
const arr = [3, 2, 0, 1];
rearrange(arr);
printArr(arr);
Related Article:
Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’
Similar Reads
Modify an array such that if 'arr[i]' is 'j' then arr[j] becomes i Given an array arr[] of size n, where all elements are distinct and fall within the range 0 to n-1. The task is to modify arr[] such that if arr[i] = j, then it gets transformed into arr[j] = i.Examples: Input: arr[] = [1, 3, 0, 2]Output: 2 0 3 1Explanation: Since arr[0] = 1, update arr[1] to 0Since
13 min read
Rearrange an array such that arr[i] = i Given an array of elements of length n, ranging from 0 to n - 1. All elements may not be present in the array. If the element is not present then there will be -1 present in the array. Rearrange the array such that arr[i] = i and if i is not present, display -1 at that place.Examples: Input: arr[] =
13 min read
Rearrange given array such that no array element is same as its index Given an array arr[] consisting of N distinct integers, the task is to rearrange the array such that no element is same as its index ( 1-based indexing ). If multiple solutions exist, print any one of them. Examples: Input: arr[] = {4, 2, 3, 1}Output: 3 1 4 2Explanation: The elements at indices {1,
7 min read
Rearrange array such that even index elements are smaller and odd index elements are greater Given an array, rearrange the array such that : If index i is even, arr[i] <= arr[i+1]If index i is odd, arr[i] >= arr[i+1] Note: There can be multiple answers. Examples: Input : arr[] = {2, 3, 4, 5} Output : arr[] = {2, 4, 3, 5} Explanation : Elements at even indexes are smaller and elements
10 min read
Rearrange sorted array such that all odd indices elements comes before all even indices element Given a sorted array arr[] consisting of N positive integers, the task is to rearrange the array such that all the odd indices elements come before all the even indices elements. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}Output: 2 4 6 8 1 3 5 7 9 Input: arr[] = {0, 3, 7, 7, 10}Output: 3 7
12 min read