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
36 changes: 36 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
//Missing Number in Sorted Array of Natural Numbers
// Solved it by binary search. intuition is that in an array of numbers starting from 1, the value at each index should be index + 1. If a number is missing, then this will become false. We find the mid and check if
// arr[mid] - 1 == mid. if it is false we move to the left half else the right half.
//Time complexity is O(log n) and space complexity is O(1).
public class Problem1 {
static int missingNumber(int arr[]) {
// code here
int n = arr.length;

if (arr[0] != 1) {
return 1;
}

if (arr[n - 1] != (n + 1)) {
return n + 1;
}

int low = 0;
int high = n -1;

while (low <= high) {
int mid = low + (high - low)/2;
if (arr[mid] - 1 != mid) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return arr[low] - 1;
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7, 8};
System.out.println(missingNumber(arr));
}
}