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.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);intn=input.nextInt();intunits=1;//Initialize the array of toysint[]toys=newint[n];for(inti=0;i<n;i++)toys[i]=input.nextInt();Arrays.sort(toys);//Sort the toys ascending by weightintcurrentWeight=toys[0];for(intweight:toys){if(!(weight<=currentWeight+4)){units++;currentWeight=weight;}}System.out.println(units);}}
Cookie support is required to access HackerRank
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 →