| Forum Home > Java > Find a missing number | ||
|---|---|---|
|
Member Posts: 11 |
how to find a missing number of a messed +1 sequence. inputs is: amount of data not missing in the sequence data not missing in the sequence ... ... ... | |
| ||
|
Member Posts: 11 |
import java.io.*; import java.util.*;
public class MISSISINGnum {
public static int find(int[] comp,int[] incomp,int length) {
outer:for(int a=0;a<length+1;a++){//control iteration for number "supposed"to exist for(int b=0;b<length;b++){//control iteration of number from the input if (comp[a]==incomp[b]){ continue outer; }//if the number that is "supposed" to exist exists,go to the next number,otherwise,that number is the missing one } return comp[a]; } return 0;//do i need really need this? }//end of find class
public static void main(String[] args) throws IOException{ Scanner in = new Scanner(new File("missingNum.txt")); PrintWriter out = new PrintWriter(new FileWriter("misNumout.txt")); int length,missing;
for(int i=0;i<5;i++){
length=in.nextInt(); int[] complete = new int[length+1]; int[] incomplete = new int[length]; for(int I=0;I<length+1;I++){ complete[I]=I+1; }//fill complete for(int I=0;I<(length);I++){ incomplete[I]=in.nextInt(); }//fill incomplete missing=find(complete,incomplete,length); out.println(missing); }//end of each case out.close(); }//end of main }//end of class
| |
| ||
|
Member Posts: 11 |
that application works for 5 test cases | |
| ||
|
Member Posts: 11 |
yeah, the test case is actually in order..... whatever, mine works for messed order too.... | |
| ||