Skip to content

Commit 4fbe524

Browse files
authored
Merge pull request #6 from Chayandas07/Chayandas07-patch-6
Create 01 December Rearrange Array Alternately
2 parents 9405631 + cf64bc6 commit 4fbe524

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution{
2+
public:
3+
// This function wants you to modify the given input
4+
// array and no need to return anything
5+
// arr: input array
6+
// n: size of array
7+
//Function to rearrange the array elements alternately.
8+
void rearrange(long long *arr, int n)
9+
{
10+
11+
long long maxele = arr[n - 1] + 1, start = 0, end = n - 1;
12+
13+
for(long long i = 0; i < n; i++){
14+
if(i % 2 == 0){
15+
arr[i] += (arr[end--] % maxele) * maxele;
16+
}
17+
else{
18+
arr[i] += (arr[start++] % maxele) * maxele;
19+
}
20+
}
21+
for(long long i = 0; i < n; i++){
22+
arr[i] /= maxele;
23+
}
24+
25+
}
26+
};

0 commit comments

Comments
 (0)