You are viewing a single comment's thread. Return to all comments →
sorting it would make it O(n logn) right? here is a O(n) solution
public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] arr = new int[10001]; int n = scan.nextInt(); for(int i = 0; i < n; i++) { arr[scan.nextInt()]++; } int ans = 0; for(int i = 0; i < 10001; i++) { if(arr[i] > 0) { i += 4; ans++; } } System.out.println(ans); }
however it takes space of array of length 10001.
Seems like cookies are disabled on this browser, please enable them to open this website
Priyanka and Toys
You are viewing a single comment's thread. Return to all comments →
sorting it would make it O(n logn) right? here is a O(n) solution
however it takes space of array of length 10001.