Created
May 29, 2018 03:13
-
-
Save thmain/ee2e630bb17c59622cc3378d8bcd8035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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