Home > Backend Development > C++ > Rearrange an array so that arr = i, using C++

Rearrange an array so that arr = i, using C++

PHPz
Release: 2023-09-13 11:25:02
forward
1104 people have browsed it

重新排列一个数组,使得 arr = i,使用 C++

We have an array of positive integer type, assuming it is arr[], its size can be given arbitrarily, and the elements in the array should be greater than 0 but less than the size of the array. The task is to rearrange the array such that if arr[i] is equal to 'i', then 'i' exists in the array, otherwise set the arr[i] element to -1 and print the final result.

Let us take a look at various input and output scenarios for this problem:

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

Output − The rearranged array is: 0 1 2 3 4 5 -1 -1

Explanation − We have an integer array of size 8, all elements in the array are 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)
Copy after login

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

output − Rearrange the array so that arr[i] = i is: -1 1 2 -1 -1

Explanation− We get an integer array of size 5, and All elements in the array have values ​​less than or greater 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)
Copy after login

The method used in the program below is as follows:

  • Enter an array of integer type and calculate the size of the array.

  • Print the array before sorting and call the function Rearranging(arr, size)

  • Inside the function Rearranging(arr, size)

    • Declare an integer type variable, assuming it is ptr

    • Start looping from i to 0 until i is less than size. Inside the loop, another loop starts from j to 0 until j is less than size.

    • Inside the loop, check if arr[j] = i, then set ptr = arr[j], arr[j] = arr[i], arr[i] = ptr And break out of the loop.

    • Start looping from i to size. Inside the loop, check if arr[i]! = i, then set arr[i] to -1.

  • #Print an array after rearranging the array values.

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] << " ";
   }
}
Copy after login

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
Copy after login

The above is the detailed content of Rearrange an array so that arr = i, using C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template