MCPcopy Create free account
hub / github.com/Ayush7614/Daily-Coding-DS-ALGO-Practice / BeautifulArray

Class BeautifulArray

Leetcode/Java/BeautifulArray.java:19–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17import java.io.*;
18
19class BeautifulArray
20{
21 public int[] beautifulArray(int n)
22 {
23 ArrayList<Integer> arr = new ArrayList<>();
24 arr.add(1);
25 while(arr.size()<n)
26 {
27 ArrayList<Integer> tmp = new ArrayList<>();
28 for(int i : arr)
29 {
30 if(2*i - 1 <=n)
31 tmp.add(2*i - 1);
32 }
33 for(int i : arr)
34 {
35 if(2*i <=n)
36 tmp.add(2*i);
37 }
38 arr = tmp;
39 }
40 int ans[]= new int[arr.size()];
41 for(int i = 0; i<arr.size(); i++)
42 ans[i] = arr.get(i);
43
44 return ans;
45 }
46
47 public static void main(String args[])
48 {
49 Scanner sc = new Scanner(System.in);
50 BeautifulArray obj = new BeautifulArray();
51 int n = sc.nextInt();
52 int ans[] = obj.beautifulArray(n);
53 for(int i = 0; i<ans.length ; i++)
54 System.out.print(ans[i]+" ");
55
56 }
57}
58
59/*
60 * Time Complexity: O(N*logN)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected