forked from Michael-Calce/GeeksForGeeks-Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Chayandas07/Chayandas07-patch-6
Create 01 December Rearrange Array Alternately
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
}; |