You are viewing a single comment's thread. Return to all comments →
Java Solution
public static int pickingNumbers(List<Integer> a){ int max=0; HashMap<Integer,Integer> count=new HashMap<>(); for(Integer element:a){ if(count.containsKey(element)){ count.put(element,count.get(element)+1); }else{ count.put(element,1); } } Set<Integer> keys=count.keySet(); for(Integer key: keys){ int tempMax=0; if(count.containsKey(key-1) && count.containsKey(key+1)){ tempMax=count.get(key-1)>count.get(key+1) ? count.get(key)+count.get(key-1) : count.get(key)+count.get(key+1) ; }else if(count.containsKey(key-1) && !count.containsKey(key+1)){ tempMax=count.get(key)+count.get(key-1); }else if(!count.containsKey(key-1) && count.containsKey(key+1)){ tempMax=count.get(key)+count.get(key+1); }else{ tempMax = count.get(key); } if(tempMax>max){ max=tempMax; } } return max; }
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