Skip to content
Merged
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
26 changes: 26 additions & 0 deletions 01 December Rearrange Array Alternately
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)
{

long long maxele = arr[n - 1] + 1, start = 0, end = n - 1;

for(long long i = 0; i < n; i++){
if(i % 2 == 0){
arr[i] += (arr[end--] % maxele) * maxele;
}
else{
arr[i] += (arr[start++] % maxele) * maxele;
}
}
for(long long i = 0; i < n; i++){
arr[i] /= maxele;
}

}
};