Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 01 December Rearrange Array Alternately #6

Merged
merged 1 commit into from
Dec 1, 2022
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;
}

}
};