Created
April 14, 2021 12:50
-
-
Save samflab/40684aef1e6b33c6a0110befdf5bbf80 to your computer and use it in GitHub Desktop.
Smallest Positive Missing Number in an Unsorted Array Code
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
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