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
30 changes: 30 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)

// Perform binary search to check if a number is in the correct index
// A number is in the correct index if the number = index + 1
// If correct move to right side of array else move to left side of array

class Solution {
public int findMissingNumber(int[] arr) {
int n = arr.length;
int low = 0;
int high = n-1;
while(low <= high) {
int mid = low + (high - low) / 2;
if(arr[mid] == mid+1) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return arr[low]-1;
}

public static void main(String args[]) {
Solution obj = new Solution();
int[] arr = new int[]{1, 2, 3, 5, 6};
int missingNumber = obj.findMissingNumber(arr);
System.out.println("The missing number is: " + missingNumber);
}
}