forked from Zubair-droid/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrearrangeArrayAlternatively.cpp
More file actions
55 lines (49 loc) · 1.16 KB
/
Copy pathrearrangeArrayAlternatively.cpp
File metadata and controls
55 lines (49 loc) · 1.16 KB
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
//Rearrange array alernatively
//level medium
//company : ZOHO
//This is the most efficient approach with
// Expected Time Complexity: O(N).
// Expected Auxiliary Space: O(1).
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t, n;
//test cases
cin >> t;
while (t--)
{
//size of array
cin >> n;
long long arr[n];
//adding elements
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int max_index = n - 1;
int min_index = 0;
int max = arr[n - 1] + 1;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
arr[i] = (arr[max_index] % max) * max + arr[i];
max_index--;
}
else
{
arr[i] = (arr[min_index] % max) * max + arr[i];
min_index++;
}
}
for (int i = 0; i < n; i++)
{
arr[i] /= max;
}
//printing elements
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}
}