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

C++Server Side ProgrammingProgramming

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 arr[i] becomes arr[arr[i]] within the given O(1) space only and print the final result.

Let us see various input output scenarios for this −

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

Output − Array before Arrangement: 0 3 2 1 5 4 Rearrangement of an array so that arr[i] becomes arr[arr[i]] with O(1) extra space is: 0 1 2 3 4 5

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[arr[0] is 0, arr[arr[1]] is 1, arr[arr[2]] is 2, arr[arr[3]] is 3, arr[arr[4]] is 4 and arr[arr[5]] is 5. So, the final array after rearrangement is 0 1 2 3 4 5.

Input − int arr[] = {1, 0}

Output − Array before Arrangement: 1 0 Rearrangement of an array so that arr[i] becomes arr[arr[i]] with O(1) extra space is: 0 1

Explanation − we are given an integer array of size 2 and all the elements in an array value less than 2. Now, we will rearrange the array i.e. arr[arr[0] is 1 and arr[arr[1]] is 0. So, the final array after rearrangement is 0 1.

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

Output − Array before Arrangement: 1 0 2 3 Rearrangement of an array so that arr[i] becomes arr[arr[i]] with O(1) extra space is: 0 1 2 3

Explanation − we are given an integer array of size 4 and all the elements in an array value less than 4. Now, we will rearrange the array i.e. arr[arr[0] is 0, arr[arr[1]] is 1, arr[arr[2]] is 2 and arr[arr[3]] is 3. So, the final array after rearrangement is 0 1 2 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)

    • Start loop FOR from i to 0 till i less than size. Inside the loop, set temp as arr[arr[i]] % size and arr[i] += temp * size.

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

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int arr[], int size){
   for(int i=0; i < size; i++){
      int temp = arr[arr[i]] % size;
      arr[i] += temp * size;
   }
   for(int i = 0; i < size; i++){
      arr[i] = arr[i] / size;
   }
}
int main(){
   //input an array
   int arr[] = {0, 3, 2, 1, 5, 4};
   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 so that arr[i] becomes arr[arr[i]] with O(1) extra space 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: 0 3 2 1 5 4
Rearrangement of an array so that arr[i] becomes arr[arr[i]] with O(1) extra space is: 0 1 2 3 4 5
raja
Updated on 02-Nov-2021 06:16:33

Advertisements