From 5f113e442ca8a4e0d5e61d9b09eee8e8f7997cee Mon Sep 17 00:00:00 2001 From: kalyan Date: Mon, 6 Oct 2025 18:25:09 -0500 Subject: [PATCH] Competitive coding solutions --- Problem1.java | 25 ++++++++++++++ Problem2.java | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/Problem1.java b/Problem1.java index 8b137891..61961243 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,26 @@ +// Time Complexity : O(log n) as it's binary search. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Not much, I was intially thinking of array.length/2 and checking with mid to see which side +// of the array to go ahead with. But later realized it wont work always and got a suggestion to use indexes. One I considered index, it +//was a straight forward binary search. +// Your code here along with comments explaining your approach + + +class Solution { + int missingNumber(int arr[]) { + // code here + int low = 0; + int high = arr.length - 1; + while(low<=high){ + int mid = low + (high-low)/2; + if(arr[mid] == mid+1){ //right part from mid + low = mid+1; + }else{ //left part from mids + high = mid-1; + } + } + return low+1; + } +} diff --git a/Problem2.java b/Problem2.java index 8b137891..8eb4fe05 100644 --- a/Problem2.java +++ b/Problem2.java @@ -1 +1,91 @@ +// Time Complexity : insert and extract are O(log n). +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Approach : In min heap, the root node is smaller than or equal to the child nodes. In the insert function, we add the new element +// to the end of the array and then bubble up comparing with nodes and so on. In extractMin function, we remove the 0th element as it +// will be minimum always, assign the last value of array and heapify down the tree by comparing the values. +// Your code here along with comments explaining your approach + + + +public class MinHeap { + private int[] heap; + private int size; + private int capacity; + + public MinHeap(int capacity) { + this.capacity = capacity; + this.heap = new int[capacity]; + this.size = 0; + } + + private int parent(int i) { return (i - 1) / 2; } + private int left(int i) { return 2 * i + 1; } + private int right(int i) { return 2 * i + 2; } + + private void swap(int i, int j) { + int temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + + // Insert new value + public void insert(int val) { + if (size == capacity) return; + heap[size] = val; + int current = size; + size++; + + // Bubble up + while (current > 0 && heap[current] < heap[parent(current)]) { + swap(current, parent(current)); + current = parent(current); + } + } + + // Extract the smallest element (root) + public int extractMin() { + if (size == 0) return -1; + int min = heap[0]; //0th is minimum + heap[0] = heap[size - 1]; + size--; + heapifyDown(0); + return min; + } + + private void heapifyDown(int i) { + int smallest = i; + int left = left(i); + int right = right(i); + + if (left < size && heap[left] < heap[smallest]) + smallest = left; + if (right < size && heap[right] < heap[smallest]) + smallest = right; + + if (smallest != i) { + swap(i, smallest); + heapifyDown(smallest); //continue the swapping logic + } + } + + public void printHeap() { + for (int i = 0; i < size; i++) { + System.out.print(heap[i] + " "); + } + System.out.println(); + } + + // Test + public static void main(String[] args) { + MinHeap heap = new MinHeap(10); + heap.insert(5); + heap.insert(3); + heap.insert(8); + heap.insert(1); + heap.printHeap(); // may print like: 1 3 8 5 (heap order, not sorted) + System.out.println("Extract Min: " + heap.extractMin()); // 1 + heap.printHeap(); // heap rearranged + } +}