We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.Arrays;importjava.util.Scanner;publicclassHackerlandRadioTransmitters{staticinthackerlandRadioTransmitters(int[]x,intk){Arrays.sort(x);intn=x.length;inttransmitters=0;inti=0;while(i<n){transmitters++;intloc=x[i]+k;// Move to the rightmost house within rangewhile(i<n&&x[i]<=loc){i++;}// Move to the leftmost house outside rangeloc=x[--i]+k;while(i<n&&x[i]<=loc){i++;}}returntransmitters;}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);// Read inputintn=scanner.nextInt();intk=scanner.nextInt();int[]x=newint[n];for(inti=0;i<n;i++){x[i]=scanner.nextInt();}// Print outputSystem.out.println(hackerlandRadioTransmitters(x,k));scanner.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hackerland Radio Transmitters
You are viewing a single comment's thread. Return to all comments →