Skip to content

Commit

Permalink
Merge pull request #6 from Chayandas07/Chayandas07-patch-6
Browse files Browse the repository at this point in the history
Create 01 December  Rearrange Array Alternately
  • Loading branch information
Chayandas07 authored Dec 1, 2022
2 parents 9405631 + cf64bc6 commit 4fbe524
Showing 1 changed file with 26 additions and 0 deletions.
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;
}

}
};

0 comments on commit 4fbe524

Please sign in to comment.