
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange an array such that arr[i] = i 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[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
- Related Articles
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Rearrange an Array to Maximize i*arr[i] using 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++
- 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++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in 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++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
