- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++

We are given a positive integer type array, let's say, arr[] of any given size such that elements in an array should value greater than 0 but less than the size of an array. The task is to rearrange an array in such a manner that if arr[j] is ‘j’ then arr[j] becomes ‘i’ and print the final result.

## Let us see various input output scenarios for this −

**Input **− int arr[] = {3, 4, 1, 2, 0}

**Outpu t**− Array before Arrangement: 3 4 1 2 0
Rearrangement of an array such that arr[j] becomes i if arr[i] is j is: 4 2 3 0 1

**Explanation **− we are given an integer array of size 6 and all the elements in an array value less than 6. Now, we will rearrange the array i.e. arr[1] is 4, arr[4] = 1 ; arr[2] is 1, arr[1] = 2; arr[3] is
2, arr[2] = 3; arr[4] is 0, arr[0] =4. Therefore, the final array is 4 2 3 0 1.

**Inpu t**− int arr[] = {2, 0, 1, 3}

**Output **− Array before Arrangement: 2 0 1 3
Rearrangement of an array such that arr[j] becomes i if arr[i] is j is: 1 2 0 3

**Explanation **− we are given an integer array of size 6 and all the elements in an array value less than 6. Now, we will rearrange the array i.e. arr[0] is 2, arr[2] = 0 ; arr[1] is 0, arr[0] = 1; arr[2] is
1, arr[1] = 2; arr[3] is 3, arr[3] = 3. Therefore, the final array is 1 2 0 3.

## Approach used in the below program is as follows

Input an array of integer type elements and calculate the size of an array.

Print the array before arrangement and call the function Rearrangement(arr, size)

Inside the function Rearrangement(arr, size)

Create an array of integer type values as ptr[] of the same size as of array arr[].

Start loop FOR, from i to 0 till i less than size. Inside the loop, set ptr[arr[i]] to i.

Start loop FOR from i to 0 till i less than size. Inside the loop, set arr[i] to ptr[i].

Print the array after the rearrangement of values of an array.

## Example

#include <bits/stdc++.h> using namespace std; void Rearrangement(int arr[], int size){ int ptr[size]; for(int i = 0; i < size; i++){ ptr[arr[i]] = i; } for(int i = 0; i < size; i++){ arr[i] = ptr[i]; } } int main(){ //input an array int arr[] = {3, 4, 1, 2, 0}; int size = sizeof(arr) / sizeof(arr[0]); //print the original Array cout<<"Array before Arrangement: "; for (int i = 0; i < size; i++){ cout << arr[i] << " "; } //calling the function to rearrange the array Rearrangement(arr, size); //print the array after rearranging the values cout<<"\nRearrangement of an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ is: "; for(int i = 0; i < size; i++){ cout<< arr[i] << " "; } return 0; }

## Output

If we run the above code it will generate the following Output

Array before Arrangement: 3 4 1 2 0 Rearrangement of an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ is: 4 2 3 0 1

- Related Questions & Answers
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Rearrange an array such that arr[i] = i in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Rearrange an Array to Maximize i*arr[i] using C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++