Created
August 26, 2024 18:20
-
-
Save yashgyy/b16881915df3462df236ef6cb5f70870 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Example [1,2,4,6,3,7,8] Output: 5 | |
We know Sum of first n natural numbers = n ( n+1) / 2 | |
All the numbers are distinct starting from 1 and are in range 1 to n | |
int findMissingNumber(int A[], int n) { | |
int i = 0 | |
for (int j = 0; j<A.length ; j++){ | |
i = i + A[j] | |
} | |
int missing_no = n * (n+1) / 2 - i | |
return missing_no | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment