-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrearrange_array_alternately.cpp
80 lines (62 loc) · 1.84 KB
/
rearrange_array_alternately.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// solution is in class solution
// C++ program to rearrange an array in minimum maximum form
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
// This function wants you to modify the given input
// array and no need to return anything
// arr: input array
// n: size of array
//Function to rearrange the array elements alternately.
void rearrange(long long *arr, int n)
{
int max_idx = n - 1, min_idx = 0;
int max_elem = arr[n - 1] + 1;
for (int i = 0; i < n; i++) {
//At even index, we have to put maximum elements in decreasing order.
if (i % 2 == 0) {
arr[i] += (arr[max_idx] % max_elem) * max_elem;
//Updating maximum index.
max_idx--;
}
//At odd index, we have to put minimum elements in increasing order.
else {
arr[i] += (arr[min_idx] % max_elem) * max_elem;
//Updating minimum index.
min_idx++;
}
}
//Dividing array elements by maximum element to get the result.
for (int i = 0; i < n; i++)
arr[i] = arr[i] / max_elem;
}
};
// { Driver Code Starts.
// Driver program to test above function
int main()
{
int t;
//testcases
cin >> t;
while(t--){
//size of array
int n;
cin >> n;
long long arr[n];
//adding elements to the array
for(int i = 0;i<n;i++){
cin >> arr[i];
}
Solution ob;
//calling rearrange() function
ob.rearrange(arr, n);
//printing the elements
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends