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.
for i = 0 in a as soon as i is less than the size of a
create a new temporal list (this will reset when i changes value to not count the previous item)
add to the temp the value of a at position i
compare i with i+1( j ), so: for j = i+1 as soon as j is less than size of a
if the absolute value of a at pisition i minus the value of a a at position j is less than 2
add j to temporal
add to count the size of temp
then return the max value of the list (use Collections.max()
or
Sort the count list
return counter at last index position
publicstaticintpickingNumbers(List<Integer>a){// Write your code hereCollections.sort(a);List<Integer>count=newArrayList<>();for(inti=0;i<a.size()-1;i++){List<Integer>temp=newArrayList<>();temp.add(a.get(i));for(intj=i+1;j<a.size();j++){if(Math.abs(a.get(i)-a.get(j))<2){temp.add(j);}}count.add(temp.size());}returnCollections.max(count);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Picking Numbers
You are viewing a single comment's thread. Return to all comments →
Java Solution with explanation
Sort the list given
Create a count list to store the subarray
for i = 0 in a as soon as i is less than the size of a
or