forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
123.cpp
40 lines (32 loc) · 777 Bytes
/
123.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
// The function to rearrange an array
// in-place so that arr[i] becomes arr[arr[i]].
void rearrange(int arr[], int n)
{
// First step: Increase all values by (arr[arr[i]]%n)*n
for (int i=0; i < n; i++)
arr[i] += (arr[arr[i]]%n)*n;
// Second Step: Divide all values by n
for (int i=0; i<n; i++)
arr[i] /= n;
}
// A utility function to print an array of size n
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
/* Driver program to test above functions*/
int main()
{
int arr[] = {3, 2, 0, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Given array is \n";
printArr(arr, n);
rearrange(arr, n);
cout << "Modified array is \n";
printArr(arr, n);
return 0;
}