Missing Number Problem HackerRank

 Given two arrays of integers, find which elements in the second array are missing from the first array.


Example



The  array is the orginal list. The numbers missing are .


Notes


If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same. If that is not the case, then it is also a missing number.

Return the missing numbers sorted ascending.

Only include a missing number once, even if it is missing multiple times.

The difference between the maximum and minimum numbers in the original list is less than or equal to .

Function Description


Complete the missingNumbers function in the editor below. It should return a sorted array of missing numbers.


missingNumbers has the following parameter(s):


int arr[n]: the array with missing numbers

int brr[m]: the original array of numbers

Returns


int[]: an array of integers

Input Format


There will be four lines of input:


 - the size of the first list, 

The next line contains  space-separated integers 

 - the size of the second list, 

The next line contains  space-separated integers 


Constraints


Sample Input


10

203 204 205 206 207 208 203 204 205 206

13

203 204 204 205 206 207 205 208 203 206 205 206 204

Sample Output


204 205 206

Explanation


 is present in both arrays. Its frequency in  is , while its frequency in  is . Similarly,  and  occur twice in , but three times in . The rest of the numbers have the same frequencies in both lists.



Solution :


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'missingNumbers' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY arr
     *  2. INTEGER_ARRAY brr
     */

    public static List<Integer> missingNumbers(List<Integer> arr, List<Integer> brr) {
    // Write your code here
   Map<Integer, Integer> freqMap = new HashMap<>();

        // Count frequencies in brr (original array)
        for (int num : brr) {
            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
        }

        // Decrease frequencies based on arr (given subset)
        for (int num : arr) {
            freqMap.put(num, freqMap.get(num) - 1);
        }

        // Find missing numbers (where count > 0)
        List<Integer> missing = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
            if (entry.getValue() > 0) {
                missing.add(entry.getKey());
            }
        }

        // Sort the missing numbers
        Collections.sort(missing);
        return missing;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int m = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> brr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        List<Integer> result = Result.missingNumbers(arr, brr);

        bufferedWriter.write(
            result.stream()
                .map(Object::toString)
                .collect(joining(" "))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence