diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 0000000..98ff0e8 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,17 @@ +def binarySearchmis(arr, l ,h) -> int: + while (l<=h): + mid = (l + h)//2 + if mid ==len(arr)-1 or (mid == arr[mid]-1 and mid + 1 == arr[mid+1] - 2): + return arr[mid] + 1 + if mid == 0 or (mid == arr[mid] - 2 and mid - 1 == arr[mid-1] - 1): + return arr[mid] - 1 + elif mid < arr[mid] - 1: + return binarySearchmis(arr, l, mid-1) + else: + return binarySearchmis(arr, mid+1, h) + return arr[h]+1 + + +arr= [2,3,4] + +print(binarySearchmis(arr,0,len(arr)-1)) diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 0000000..ce64dea --- /dev/null +++ b/Problem2.py @@ -0,0 +1,40 @@ +class minHeap: + def __init__(self): + self.heap = [] + self.size = 0 + def insert(self, data: int) -> None: + self.heap.append(data) + self.size += 1 + i = len(self.heap) - 1 + while (i //2 >= 0): + if self.heap[i] < self.heap[i//2]: + self.heap[i], self.heap[i//2] = self.heap[i//2], self.heap[i] + else: + break + i = i // 2 + def removemin(self) -> int: + if not self.heap: + return + i = 0 + while(i*2 < len(self.heap)-1): + self.heap[i], self.heap[2*i + 1] = self.heap[2*i + 1], self.heap[i] + i = 2*i + 1 + return self.heap.pop() + def getmin(self) -> int: + if not self.heap: + return + return self.heap[0] + + +Heap = minHeap() + +Heap.insert(3) +Heap.insert(2) +Heap.insert(1) + +print(Heap.getmin()) + +print(Heap.removemin()) + +print(Heap.removemin()) + \ No newline at end of file