Question
Here is what I have been given: package exam1; public class FixFaulty { public static int[] findMissingIntegers(int n, int[] faultysOutput) { int[] answer = new
Here is what I have been given:
package exam1;
public class FixFaulty { public static int[] findMissingIntegers(int n, int[] faultysOutput) { int[] answer = new int[n-faultysOutput.length]; /**********************************************/ // Your code goes right here. You may use helper methods if you want to. // You are not allowed to use any built-in Java data structures such as LinkedList, ArrayList, etc. // However, you can use arrays, as you did in your previous programming courses. /**********************************************/ return answer; } }
Here is the test file to base it off of:
package exam1;
import java.util.Arrays;
public class TestFixFaulty {
// Please do not touch this method. // This method is just trying to match the expected output with the actual output. public static void match( int[] test, int[] expectedOutput, int[] actualOutput ) { if( Arrays.equals(expectedOutput, actualOutput ) ) System.out.println("Congratulations! Your code has passed the test: " + Arrays.toString(test) + ". " ); else { System.out.println("Oops! Your code has failed the test: " + Arrays.toString(test) ); System.out.println("Expected output: " + Arrays.toString(expectedOutput)); System.out.println("Actual output (output from your code): " + Arrays.toString(actualOutput) + " "); } } public static void main(String[] args) { int n; // Changes with every test input. ////////////////////Test 1//////////////////////////// int[] test1 = {9,8,5,1,2}; n = 10; int[] expectedOutput1 = {3,4,6,7,10}; int[] actualOutput1 = FixFaulty.findMissingIntegers(n,test1); match(test1, expectedOutput1, actualOutput1); ////////////////////////////////////////////////
////////////////////Test 2//////////////////////////// int[] test2 = {2,4,5,7,8,9,10,11,13}; n = 13; int[] expectedOutput2 = {1,3,6,12}; int[] actualOutput2 = FixFaulty.findMissingIntegers(n,test2); match(test2, expectedOutput2, actualOutput2); ////////////////////////////////////////////////
////////////////////Test 3//////////////////////////// int[] test3 = {10,9,8,7,6,5}; n = 10; int[] expectedOutput3 = {1,2,3,4}; int[] actualOutput3 = FixFaulty.findMissingIntegers(n,test3); match(test3, expectedOutput3, actualOutput3); ////////////////////////////////////////////////
////////////////////Test 4//////////////////////////// int[] test4 = {7}; n = 15; int[] expectedOutput4 = {1,2,3,4,5,6,8,9,10,11,12,13,14,15}; int[] actualOutput4 = FixFaulty.findMissingIntegers(n,test4); match(test4, expectedOutput4, actualOutput4); ////////////////////////////////////////////////
////////////////////Test 5//////////////////////////// int[] test5 = {1,3,5,7,9}; n = 11; int[] expectedOutput5 = {2,4,6,8,10,11}; int[] actualOutput5 = FixFaulty.findMissingIntegers(n,test5); match(test5, expectedOutput5, actualOutput5); //////////////////////////////////////////////// System.out.println("*** Reminder *** for grading, we may use different tests."); System.out.println("So, use other tests too for testing your code."); } }
Question Prompt:
Say you give an integer n to it and ask it to count from 1 to n. Assume that 1 n 5000. It will try to count the integers for you but the integer ordering may get scrambled and further, it may miss some of the integers. Here is an example. Say n = 10. Faultie may count the integers like this: 9, 8, 5, 1, 2. But we were expecting 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 from it. So as you have seen, not only it scrambled the ordering but also forgot quite a few numbers. But one assurance is that Faultie never repeats an integer; it says an integer exactly once. Your task is to write a program that will find out the numbers Faultie has missed and put them in sorted order. So, for the above example, your program should obtain the 1 following sequence: 3, 4, 6, 7, 10. Observe that not only the missing numbers are obtained but also they are put in sorted order. Hint: there is an O(n) algorithm for this problem. Although your algorithm may have a worse time complexity, thinking about a O(n) time algorithm should be fun! No extra points for this though. This coding problem does not require any kind of file handling. This project has two classes only: HelpFaulty and TestHelpFaulty. For more information and sample tests, refer to the code outline. Make sure you understand the sample inputs and outputs well before you start coding
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started