From 3560b5ba1a7d31263b52e91b56d6174855942f76 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Tue, 17 Feb 2026 16:12:27 -0800 Subject: [PATCH] Done Competitive-Coding-1 --- DesignMinHeap.java | 119 +++++++++++++++++++++++++++++++++++++++++++++ MissingNumber.java | 38 +++++++++++++++ Problem1.java | 1 - Problem2.java | 1 - 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 DesignMinHeap.java create mode 100644 MissingNumber.java delete mode 100644 Problem1.java delete mode 100644 Problem2.java diff --git a/DesignMinHeap.java b/DesignMinHeap.java new file mode 100644 index 00000000..0f66a686 --- /dev/null +++ b/DesignMinHeap.java @@ -0,0 +1,119 @@ +// Time Complexity : O(log n) for insert, remove +// Space Complexity : O(n) to store n elements +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +/* +Use an array to store the incoming elements. To get parent, its i-1/2, to get left & right children, its +2*n+1 and 2*n+2,make sure to keep check of size value and doesnt cross maxSize.The logic of heapify is +that for any input, as long as its not a leaf, check if the left and right children are smaller than the +input, if so, swap them with the input and heapify recursively until smallest element stays at top. For +insert and rmeove methods as well, make sure to heapify and check smallest element is at the top. + */ +class MyMinHeap { + private int Heap[]; + private int size; + private int maxSize; + + public MyMinHeap(int capacity) { + this.maxSize = capacity; + this.size = 0; + this.Heap = new int[capacity]; + } + + private int parent(int i) { + return (i - 1) / 2; + } + + private int leftChild(int i) { + return (2 * i) + 1; + } + + private int rightChild(int i) { + return (2 * i) + 2; + } + + private boolean isLeaf(int i) { + return i >= size / 2 && i < size; + } + + private void swap(int i, int j) { + int temp = Heap[i]; + Heap[i] = Heap[j]; + Heap[j] = temp; + } + + private void heapify(int i) { + if(isLeaf(i)) + return; + int leftChild = leftChild(i); + int rightChild = rightChild(i); + + int smallest = i; + + if(leftChild > 0 && Heap[leftChild] < Heap[smallest]) + smallest = leftChild; + if(rightChild > 0 && Heap[rightChild] < Heap[smallest]) + smallest = rightChild; + + if(smallest != i) { + swap(smallest, i); + heapify(smallest); + } + } + + public void insert(int val) { + if(size >= maxSize) + return; + Heap[size] = val; + int curr = size; + size++; + + while(curr > 0 && Heap[curr] < Heap[parent(curr)]) { + swap(curr, parent(curr)); + curr = parent(curr); + } + } + + public int removeMin() { + if(size == 0) + return -1; + + int min = Heap[0]; + Heap[0] = Heap[size - 1]; + size--; + heapify(0); + return min; + } + + public void printHeap() { + for (int i = 0; i <= (size - 2) / 2; i++) { + System.out.print("PARENT: " + Heap[i]); + if (leftChild(i) < size) + System.out.print(" LEFT: " + Heap[leftChild(i)]); + if (rightChild(i) < size) + System.out.print(" RIGHT: " + Heap[rightChild(i)]); + System.out.println(); + } + } + + public static void main(String[] args) { + MyMinHeap heap = new MyMinHeap(15); + + heap.insert(5); + heap.insert(3); + heap.insert(17); + heap.insert(10); + heap.insert(84); + heap.insert(19); + heap.insert(6); + heap.insert(22); + heap.insert(9); + + System.out.println("Min Heap:"); + heap.printHeap(); + + System.out.println("Removed Min: " + heap.removeMin()); + } +} diff --git a/MissingNumber.java b/MissingNumber.java new file mode 100644 index 00000000..bb381d88 --- /dev/null +++ b/MissingNumber.java @@ -0,0 +1,38 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +/* +Use binary search approach but the core logic to check would be if the difference of an array element +with its respective index is constant across low, mid and high indexes. If not, we update the low, high +positions depending upon where we notice the discrepancy and thereby updating the mid value as well. + */ +import java.io.*; + +class MissingNumber { + + static int search(int[] ar, int size) { + int low = 0; + int high = size - 1; + + while((high - low) > 1) { + int mid = low + (high - low) / 2; + if(ar[low] - low != ar[mid] - mid) + high = mid; + else if(ar[high] - high != ar[mid] - mid) + low = mid; + } + return ar[low] + 1; + } + + // Driver Code + public static void main(String[] args) { + int[] ar = { 1, 2, 3, 4, 5, 6, 8 }; + int size = ar.length; + + System.out.println("Missing number: " + search(ar, size)); + } +} + diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.java +++ /dev/null @@ -1 +0,0 @@ -