Rearrange an array such that arr[i] = i in 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 if arr[i] is ‘i’, if ‘i’ is present in an array else it will set the arr[i] element with the value -1 and print the final result.

Let us see various input output scenarios for this −

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

Output − Rearrangement of an array such that arr[i] = i is: 0 1 2 3 4 5 -1 -1

Explanation − we are given an integer array of size 8 and all the elements in an array value less than 8. Now, we will rearrange the array i.e.

arr[0] = 0(present in an array)
arr[1] = 1(present in an array)
arr[2] = 2(present in an array)
arr[3] = 3(present in an array)
arr[4] = 4(present in an array)
arr[5] = 5(present in an array)
arr[6] = -1(NOT present in an array)
arr[7] = -1(NOT present in an array)

Input − int arr[] = {1, 2, 6, 9, 10}

Output − Rearrangement of an array such that arr[i] = i is: -1 1 2 -1 -1

Explanation − we are given an integer array of size 5 and all the elements in an array value less than 5. Now, we will rearrange the array i.e.

arr[0] = -1(NOT present in an array)
arr[1] = 1(present in an array)
arr[2] = 2(present in an array)
arr[3] = -1(NOT present in an array)
arr[4] = -1(NOT present in an array)

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 Rearranging(arr, size)

  • Inside the function Rearranging(arr, size)

    • Declare an integer type variable let’s say, ptr

    • Start loop FOR from i to 0 till i less than size. Inside the loop, start another loop FOR from j to 0 till j less than size.

    • Inside the loop, check IF arr[j] = i then set ptr = arr[j], arr[j] to arr[i],arr[i] to ptr and break.

    • Start loop FOR from i to till i is less than size. Inside the loop, check IF arr[i]!=i then set arr[i] to -1.

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

Example

#include <iostream>
using namespace std;
void Rearranging(int arr[], int size){
   int ptr;
   for(int i = 0; i < size; i++){
      for(int j = 0; j < size; j++){
         if(arr[j] == i){
            ptr = arr[j];
            arr[j] = arr[i];
            arr[i] = ptr;
            break;
         }
      }
   }
   for(int i = 0; i < size; i++){
      if(arr[i] != i){
         arr[i] = -1;
      }
   }
}
int main(){
   int arr[] = {0, 8, 1, 5, 4, 3, 2, 9 };
   int size = sizeof(arr) / sizeof(arr[0]);
   //calling the function to rearrange an array such that arr[i] = i
   Rearranging(arr, size);
   //Printing the array
   cout<<"Rearrangement of an array such that arr[i] = i is: ";
   for(int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
}

Output

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

Rearrangement of an array such that arr[i] = i is: 0 1 2 3 4 5 -1 -1
raja
Updated on 02-Nov-2021 06:27:57

Advertisements