Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 29, 2018 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/ee2e630bb17c59622cc3378d8bcd8035 to your computer and use it in GitHub Desktop.
Save thmain/ee2e630bb17c59622cc3378d8bcd8035 to your computer and use it in GitHub Desktop.
//find the missing number from the sequence of consecutive number
//Approach is very simple, Add all the given numbers say S
//Calculate sum of N numbers by formula n(n+1)/2 , say N
//Find missing number m = N-S
public class FindMissingNumber {
int Sum;
int Sum_N;
public int missingNumber(int [] arrA, int size){
Sum_N = size*(size+1)/2;
for(int i=0;i<arrA.length;i++){
Sum +=arrA[i];
}
return Sum_N-Sum;
}
public static void main(String args[]){
int [] arrA = {1,2,3,4,5,7,8,9,10};
System.out.println("Missing number is :" + (new FindMissingNumber()).missingNumber(arrA,10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment