Skip to content

Instantly share code, notes, and snippets.

@samflab
Created April 14, 2021 12:50
Show Gist options
  • Save samflab/40684aef1e6b33c6a0110befdf5bbf80 to your computer and use it in GitHub Desktop.
Save samflab/40684aef1e6b33c6a0110befdf5bbf80 to your computer and use it in GitHub Desktop.
Smallest Positive Missing Number in an Unsorted Array Code
int findMissing(int arr[], int n) {
for(int i = 0; i < n; i++){
//looking for all the conditions
while(arr[i] >= 1 && arr[i] <= n && arr[i] != arr[arr[i]-1])
swap(arr[i], arr[arr[i]-1]);
}
//to check our missing number. For explaination, refer the article.
for(int i = 0; i < n; i++){
if(arr[i] != i + 1)
return i + 1;
}
// if array has all the values from 1 to n
return n + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment