You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array arr[] of size N where every element is in the range from 0 to n-1. Rearrange the given array so that arr[i] becomes arr[arr[i]].
3
+
4
+
Example 1:
5
+
Input:
6
+
N = 2
7
+
arr[] = {1,0}
8
+
Output: 0 1
9
+
Explanation:
10
+
arr[arr[0]] = arr[1] = 0.
11
+
arr[arr[1]] = arr[0] = 1.
12
+
13
+
Example 2:
14
+
Input:
15
+
N = 5
16
+
arr[] = {4,0,2,1,3}
17
+
Output: 3 4 2 0 1
18
+
Explanation:
19
+
arr[arr[0]] = arr[4] = 3.
20
+
arr[arr[1]] = arr[0] = 4.
21
+
and so on.
22
+
23
+
Your Task:
24
+
You don't need to read input or print anything. The task is to complete the function arrange() which takes arr and N as input parameters and rearranges the elements in the array in-place.
25
+
26
+
Expected Time Complexity: O(N)
27
+
Expected Auxiliary Space: O(1)
28
+
29
+
Constraints:
30
+
1 <= N <= 107
31
+
0 <= Arr[i] < N */
32
+
33
+
//Solution -:
34
+
#include<bits/stdc++.h>
35
+
usingnamespacestd;
36
+
37
+
38
+
// } Driver Code Ends
39
+
40
+
41
+
classSolution{
42
+
public:
43
+
// arr: input array
44
+
// n: size of array
45
+
//Function to rearrange an array so that arr[i] becomes arr[arr[i]]
0 commit comments