|
| 1 | +import java.io.*; |
| 2 | +import java.math.*; |
| 3 | +import java.security.*; |
| 4 | +import java.text.*; |
| 5 | +import java.util.*; |
| 6 | +import java.util.concurrent.*; |
| 7 | +import java.util.regex.*; |
| 8 | + |
| 9 | +public class solution { |
| 10 | + |
| 11 | + // Complete the missingNumbers function below. |
| 12 | + static int[] missingNumbers(int[] arr, int[] brr) { |
| 13 | + |
| 14 | + //Create an empty TreeMap to store array elements and their frequencies. |
| 15 | +TreeMap<Integer, Integer> integerFreqMap = new TreeMap<>(); |
| 16 | + |
| 17 | + // Add elements of original list |
| 18 | + //Traverse the array brr, and update the map with the frequency of each element. |
| 19 | + //This map now represents all the elements of the original array. Note that they are also in a sorted manner. |
| 20 | + for (int i : brr) { |
| 21 | + int freq = integerFreqMap.getOrDefault(i, 0); |
| 22 | + freq++; |
| 23 | + integerFreqMap.put(i, freq); |
| 24 | + } |
| 25 | + |
| 26 | + // Remove elements of new list |
| 27 | + //Now traverse the array arr and for each element in the array, decrease the frequency by 1. |
| 28 | + for (int i : arr) { |
| 29 | + int freq = integerFreqMap.get(i); |
| 30 | + freq--; |
| 31 | + if (freq == 0) //If the frequency of any element becomes 0, then remove the key from the TreeMap. |
| 32 | + integerFreqMap.remove(i); |
| 33 | + else |
| 34 | + integerFreqMap.put(i, freq); |
| 35 | + } |
| 36 | + |
| 37 | + // Create the result array |
| 38 | + int[] result = new int[integerFreqMap.size()]; |
| 39 | + int i = 0; |
| 40 | + //At the end, scan the entire map for one last time and see all the elements who have a frequency greater than 1. |
| 41 | + for (Map.Entry<Integer, Integer> integerIntegerEntry : integerFreqMap.entrySet()) { |
| 42 | + result[i++] = integerIntegerEntry.getKey(); |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + private static final Scanner scanner = new Scanner(System.in); |
| 50 | + |
| 51 | + public static void main(String[] args) throws IOException { |
| 52 | + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); |
| 53 | + |
| 54 | + int n = scanner.nextInt(); |
| 55 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 56 | + |
| 57 | + int[] arr = new int[n]; |
| 58 | + |
| 59 | + String[] arrItems = scanner.nextLine().split(" "); |
| 60 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 61 | + |
| 62 | + for (int i = 0; i < n; i++) { |
| 63 | + int arrItem = Integer.parseInt(arrItems[i]); |
| 64 | + arr[i] = arrItem; |
| 65 | + } |
| 66 | + |
| 67 | + int m = scanner.nextInt(); |
| 68 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 69 | + |
| 70 | + int[] brr = new int[m]; |
| 71 | + |
| 72 | + String[] brrItems = scanner.nextLine().split(" "); |
| 73 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 74 | + |
| 75 | + for (int i = 0; i < m; i++) { |
| 76 | + int brrItem = Integer.parseInt(brrItems[i]); |
| 77 | + brr[i] = brrItem; |
| 78 | + } |
| 79 | + |
| 80 | + int[] result = missingNumbers(arr, brr); |
| 81 | + |
| 82 | + for (int i = 0; i < result.length; i++) { |
| 83 | + bufferedWriter.write(String.valueOf(result[i])); |
| 84 | + |
| 85 | + if (i != result.length - 1) { |
| 86 | + bufferedWriter.write(" "); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + bufferedWriter.newLine(); |
| 91 | + |
| 92 | + bufferedWriter.close(); |
| 93 | + |
| 94 | + scanner.close(); |
| 95 | + } |
| 96 | +} |
0 commit comments