diff --git a/Problem1.cpp b/Problem1.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem1.java b/Problem1.java index 8b137891..cb0572ca 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,35 @@ +// Time Complexity :O(logn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :No +// Any problem you faced while coding this :No + +// Your code here along with comments explaining your approach +//saw the diff between the index and val is constant or not. if not then the number is missing + +import java.io.*; + +class GFG { + + static int search(int[] ar, int size) { + int low =0; + int high = size-1; + + while (high-low >= 2){ + int mid = low + (high-low)/2; + int midDiff = ar[mid]- mid; + int lowDiff = ar[low]-low; + int highDiff = ar[high]-high; + + if (lowDiff == highDiff){ + return -1; + } + if (midDiff != lowDiff){ + high = mid; + }else if (midDiff != highDiff){ + low = mid; + } + } + return (ar[low] + ar[high])/2; + + }} diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..0e320531 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,20 @@ +# Time Complexity :O(logn) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :No +# Any problem you faced while coding this :No + + +# Your code here along with comments explaining your approach +#saw the diff between the index and val is constant or not. if not then the number is missing + +def missingElement(nums): + low = 0 + high = len(nums)-1 + while high-low >= 2: + mid = (low + high)//2 + if (mid - nums[mid] != low - nums[low]): + high = mid + else: + low = mid + return (nums[low] + nums[high])/2 +print(missingElement([1,2,3,4,5,7,8])) \ No newline at end of file diff --git a/Problem2.cpp b/Problem2.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.java b/Problem2.java index 8b137891..1f35b357 100644 --- a/Problem2.java +++ b/Problem2.java @@ -1 +1,94 @@ +// Time Complexity :insert - O(n), heapify-O(n), removemin-O(n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :No +// Any problem you faced while coding this :No + +// Your code here along with comments explaining your approach +//for remove min..i removed the first val, and then replaced the last with first and then heapify +//for insert..i inserted first then do at last and then check with parent and iterate +// for heapify..select an index and check with right and left child, then swap + +class MyMinHeap { + private int[] heap; + private int size; + private int maxSize; + + public MyMinHeap(int capacity) { + this.size = 0; + this.maxSize = capacity; + this.heap = new int[capacity]; + } + + public int parent(int i){ + return (i-1)/2; + } + + public int leftChild(int i){ + return 2*i+1; + } + + public int rightChild(int i){ + return 2*i+2; + } + + public void swap(int i, int j){ + int temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + public boolean isLeaf(int i){ + return i >= size/2 && i < size; + } + + public void insert(int val){ + if (size >= maxSize){ + return; + } + heap[size] = val; + int curr = size; + size += 1; + while (curr > 0){ + if (heap[curr] < heap[parent(curr)]){ + swap(curr,parent(curr)); + curr = parent(curr); + } + } + } + + public void heapify(int i){ + if (size >= maxSize){ + return; + } + if (isLeaf(i)){ + return; + } + int smallest = i; + if (rightChild(i) < size && heap[smallest]> heap[rightChild(i)]){ + smallest = rightChild(i); + } + if (leftChild(i) < size && heap[smallest]> heap[leftChild(i)]){ + smallest = leftChild(i); + } + if (smallest != i){ + swap(smallest,i); + heapify(smallest); + } + + } + + public int removeMin(){ + if (size ==0){ + return -1; + } + int min_val = heap[0]; + heap[0] = heap[size-1]; + size--; + heapify(0); + return min_val; + } + + + + +} diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..1e25e96f --- /dev/null +++ b/Problem2.py @@ -0,0 +1,69 @@ +# Time Complexity :insert - O(n), heapify-O(n), removemin-O(n) +# Space Complexity :O(n) +# Did this code successfully run on Leetcode :No +# Any problem you faced while coding this :No + + +# Your code here along with comments explaining your approach +#for remove min..i removed the first val, and then replaced the last with first and then heapify +#for insert..i inserted first then do at last and then check with parent and iterate +# for heapify..select an index and check with right and left child, then swap + +class MyMinHeap: + def __init__(self, capacity): + self.maxSize = 0 + self.capacity = capacity + self.heap = [] * capacity + def parent(self,i): + return (i-1/2) + def leftChild(self,i): + return (2*i + 1) + def rightChild(self,i): + return (2*i+2) + def swap(self,i,j): + self.heap[i], self.heap[j] = self.heap[j],self.heap[i] + def isleaf(self,i): + return i >= self.maxSize/2 and i < self.maxSize + + def insert(self,val): + if self.maxSize >= self.capacity: + return + self.heap[self.maxSize] = val + self.maxSize += 1 + curr = self.maxSize + heap_length = len(self.heap) + while heap_length >0: + if self.heap[curr] < self.parent(curr): + self.swap(val, self.parent(curr)) + curr = self.parent(curr) + def heapify(self,i): + if self.maxSize >= self.capacity: + return + if self.isleaf(i): + return + curr = i + right = self.heap[self.rightChild(curr)] + left = self.heap[self.leftChild(curr)] + if self.heap[curr] > right: + curr = right + if self.heap[curr] > left: + curr = left + if i != curr: + self.swap(i,curr) + self.heapify(curr) + def removeMin(self): + if self.maxSize == 0: + return + min_val = self.heap[0] + self.heap[0] = self.heap[-1] + self.size -= 1 + self.heapify(0) + return min_val + + + + + + + + \ No newline at end of file