Skip to content

Instantly share code, notes, and snippets.

@yashgyy
Created August 26, 2024 18:20
Show Gist options
  • Save yashgyy/b16881915df3462df236ef6cb5f70870 to your computer and use it in GitHub Desktop.
Save yashgyy/b16881915df3462df236ef6cb5f70870 to your computer and use it in GitHub Desktop.
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