diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..3ce8eaea --- /dev/null +++ b/Problem1.py @@ -0,0 +1,20 @@ +class Solution: + def missingNumber(self, arr): + # code here + if len(arr) == 1: + return len(arr) + + low, high = 0 , len(arr)-1 + while low < high: + mid = (low+high)//2 + #verify left half + if arr[mid] == mid+1: #O(c) + low = mid + 1 + else: #verify right half + high = mid - 1 + + if (low == 0 and arr[low] == 1) or arr[low] == low+1: + return arr[low] + 1 + + return arr[low] - 1 + diff --git a/README.md b/README.md index d3394c1a..306b32ab 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # Competitive-Coding-1 Please submit the interview problems posted in slack channel here. The problems and statements are intentionally not shown here so that students are not able to see them in advance + +Missing Number in Sorted Array of Natural Numbers +Difficulty: EasyAccuracy: 55.26%Submissions: 2K+Points: 2 +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. + +Examples: + +Input: arr[] = [1, 2, 3, 4, 6, 7, 8] +Output: 5 +Explanation: The missing integer in the array is 5. +Input: arr[] = [1, 2, 3, 4, 5, 6, 8, 9] +Output: 7 +Explanation: The missing integer in the array is 7. +Constraints: +1 ≤ arr.size() ≤ 105 +1 ≤ arr[i] ≤ arr.size()+1 + +