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
21 changes: 21 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
// Given a sorted array arr[] of n-1 integers,
// these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array.
// Find the missing integer.


class Solution {
int missingNumber(int[] arr, int n) {
int low = 0, high = n - 2; // since size = n-1

while (low <= high) {
int mid = low + (high - low) / 2;

// Expected value at mid index = mid + 1
if (arr[mid] == mid + 1)
low = mid + 1;
else
high = mid - 1;
}

return low + 1; // the missing number
}
}