Smallest missing number
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.
Examples :
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
Output: 3
Discussion :
We will modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison.
Examples :
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
Output: 3
Discussion :
We will modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison.
int firstMissing(int array[], int start, int end){ if (start > end) return end + 1; if (start != array[start]) return start; int mid = (start + end) / 2; if (array[mid] == mid) return firstMissing(array, mid+1, end); return firstMissing(array, start, mid); }
Comments
Post a Comment