Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions C++/DataStructure/rearrangeArrayAlternatively.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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";
}
}