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.
My Java Solution below has o(n + m + u + k log k) time and o(n + m +k) space:
publicstaticList<Integer>missingNumbers(List<Integer>arr,List<Integer>brr){//goal: determine which elements in brr are missing or underrepresented in arr//use a freq map for each val in brr and arrMap<Integer,Integer>brrFreqMap=brr.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.collectingAndThen(Collectors.counting(),Long::intValue)));Map<Integer,Integer>arrFreqMap=arr.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.collectingAndThen(Collectors.counting(),Long::intValue)));//iterate over each key in arr and check if its in brrSet<Integer>missingNumbersSet=newHashSet<>();for(Map.Entry<Integer,Integer>entry:brrFreqMap.entrySet()){intkey=entry.getKey();intfreqInBrr=entry.getValue();intfreqInArr=arrFreqMap.getOrDefault(key,0);// avoids null issuesif(freqInArr<freqInBrr){missingNumbersSet.add(key);}}// Step 3: Convert to list and sortList<Integer>missingNumbers=newArrayList<>(missingNumbersSet);Collections.sort(missingNumbers);returnmissingNumbers;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Missing Numbers
You are viewing a single comment's thread. Return to all comments →
My Java Solution below has o(n + m + u + k log k) time and o(n + m +k) space: