class Solution { public boolean canArrange(int[] arr, int k) { //HashMap and heap Q-3 int frequency[] = new int[k]; //find remainders for(int val : arr){ int rem = val%k; //if -ve add k to it if(rem < 0) rem +=k; frequency[rem]++; } //check all rem and [k-rem] if(frequency[0] %2 != 0) return false; for(int i = 1 ; i <= k/2 ; i++){ if(frequency[i] != frequency[k-i]) return false; } return true; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter