Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 16: Write a C++ program to rearrange an array in such a manner that arr[i] will be equal to arr[ arr[i] ].

#include<iostream>
using namespace std;

int main()
{
	int size=0;
	cout<<"\nenter the size of an array : ";
	cin>>size;
	int arr[size],arr1[size];
	cout<<"\nenter the elements of an array : ";

	for(int i=0;i<size;i++)
	{
		cin>>arr[i];
	}

	for(int i=0;i<size;i++)
	{
		arr1[i]=arr[arr[i]];
	}
	cout<<"\nnew array after rearrange :\n";

	for(int i=0;i<size;i++)
	{
		cout<<arr1[i]<<"\t";
	}
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.