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
26 changes: 26 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
//Time Complexity: O(log n)
//Space Complexity: O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no

// Before the missing element: arr[i] == i+1
// After the missing index: arr[i] == i+2
// So the missing number is i+1 at the first index where this occurs.


class Problem1{
int missingNumber(int arr[]){
int low,high,mid;
low=0;
high = arr.length-1;
while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==mid+1){
low=mid+1;//missing element is in right side
}
else{
high=mid-1;//missing element is in left side
}
}
return low+1;
}
}