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
43 changes: 43 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach

/*
I perform binary search on the sorted array to find the missing element. Each element from the missing element to the last element
would differ from it's index by 2. If I find a middle element which satisfies the condition, I store the index and search on the left half
as there might exist more elements that differ from the index by 2.
*/


class Solution
{
public int missingNumber(int[] arr)
{
// code here
int low = 0, high = arr.Length - 1;
int result = arr.Length;

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

if (arr[mid] - mid > 1)
{
result = mid;
high = mid - 1;
}

else
{
low = mid + 1;
}
}


return result + 1;
}
}