Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions FindMissingInSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

// TC: O(logn)
// SC: O(1)
// Approach:
/*
* Brute Force: Linear search if index != nums[index+1] then return that number is missing so return index+1
* Optimized: Binary Search
* if (mid + 1 == arr[mid]) that means all the elements from start to mid are present so start = mid + 1 else end = mid - 1
*/

class Solution {
int missingNumber(int arr[]) {
// code here
int start = 0;
int end = arr.length - 1;

while(start <= end){
//int overflow
int mid = start + (end - start) / 2;

if(mid + 1 == arr[mid]){
start = mid + 1;
}
else
end = mid - 1;
}
return start + 1;
}
}