[GeeksForGeeks] Find the smallest missing number - lz87 - 博客园
Given a sorted array of n distinct integers where each integer is in the range from 0 to m - 1 and m > n. Find the smallest number that is missing from the array.
Analysis:
Solution 1. O(n), linear scan;
Solution 2. O(log n), binary search: if arr[mid] > mid, then the first missing number must be in the left half; otherwise, it must be in the right half.
Solution 2 does not work if the given array can have duplicated integers.
Read full article from [GeeksForGeeks] Find the smallest missing number - lz87 - 博客园
No comments:
Post a Comment