HW2 – List the Missing Numbers

DUE: Friday, Sept 20 11:59pm

Solution: https://gist.github.com/bytecodeman/1b3f9c844ab11d648fdca0e22d717265

A data scientist had two lists that were permutations of one another. Unfortunately, while uploading them from one computer to another to another, some numbers were lost out of the first list. Find the missing numbers.

As an example, consider the array with some numbers missing, arr = [7,2,5,3,5,3], and the original array of numbers brr=[7,2,5,4,6,3,5,3]. The numbers missing are [4,6] .

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.
  • You have to print all the missing numbers in ascending order.
  • Print each missing number once, even if it is missing multiple times.

Function Description

Create a missingNumbers function. It should return a sorted array of missing numbers. missingNumbers has the following parameter(s):

  • arr: the array with missing numbers
  • brr: the original array of numbers

Input Format

There will be four lines of input:

  • n – the size of the first list, arr
  • The next line contains n space-separated integers arr[i]
  • m – the size of the second list
  • The next line contains m space-separated integers brr[i]
  • All numbers in the file will be positive.

Output Format

Output the missing numbers in ascending order.

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

Test your code by entering the examples given above using the keyboard. Once you believe the code is correct, modify the code to read a file. Download this following file, place it in your project, and run your code on the file:

You are probably going to need to use linear searching in this app, so be sure to download and add the Ultimate Linear Search Class to your project.

Use the Template.

Sample Code

How to return an array of unknown size:

import java.util.Arrays;

public class SampleCode {
  private static int[]generateJunk(int count) {
    int values[] = new int[count];
    for (int i = 0; i < values.length; i++) 
      values[i] = (int) (Math.random() * 100) + 1;	
    return values;
  }
  
  private static int[] multiplesOfN(int[] arr, int n) {
    int values[] = new int[arr.length];
    int count = 0;
    for (int i = 0; i < arr.length; i++) {
      if (arr[i] % n == 0) 
	values[count++] = arr[i];
    }
    return Arrays.copyOf(values, count);
  }

  public static void main(String[] args) {
    int junk[] = generateJunk(100);
    int multiples[] = multiplesOfN(junk, 7);
    System.out.println(Arrays.toString(multiples));
  }
}

How to read a file: (Unfortunately you need global variables to return more than 1 array based on what we know now. We’ll fix that problem later in the semester.)

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class SampleCode {
  private static int arr[];
  private static int brr[];

  private static void readFile(Scanner sc) {
    try {
      System.out.print("Enter Filename: ");
      String filename = sc.nextLine();
      Scanner fileInput = new Scanner(new File(filename));
      int aSize = fileInput.nextInt();
      arr = new int[aSize];
      for (int i = 0; i < arr.length; i++)
	arr[i] = fileInput.nextInt();
      int bSize = fileInput.nextInt();
      brr = new int[bSize];
      for (int i = 0; i < brr.length; i++)
	brr[i] = fileInput.nextInt();
      fileInput.close();
    } 
    catch (Exception ex) {
    }
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    readFile(sc);
    System.out.println(Arrays.toString(arr));
    System.out.println(Arrays.toString(brr));
}