forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeautifulArray.java
36 lines (32 loc) · 928 Bytes
/
BeautifulArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int[] BeautifulArray(int n) {
int[] ans = new int[n];
for(int i = 0; i < n; i++){
ans[i] = i+1;
}
recursion(ans, 0, n-1);
return ans;
}
public void recursion(int[] arr, int left, int right){
if(left >= right)
return;
ArrayList<Integer> l = new ArrayList<>();
ArrayList<Integer> r = new ArrayList<>();
boolean alt = true;
for(int i = left; i <= right; i++){
if(alt)
l.add(arr[i]);
else
r.add(arr[i]);
alt = !alt;
}
for(int i = left; i <= right; i++){
if(!l.isEmpty())
arr[i] = l.remove(0);
else
arr[i] = r.remove(0);
}
recursion(arr, left, (right+left)/2);
recursion(arr, (left+right)/2+1, right);
}
}