From 707f3cd4926f51924b2c322c1f0199a5c90b1f0a Mon Sep 17 00:00:00 2001 From: mukul Date: Mon, 19 Jan 2026 21:34:37 +0530 Subject: [PATCH] Done with competitve coding-2 --- Problem1.java | 32 ++++++++++++++ Problem2.java | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/Problem1.java b/Problem1.java index 8b137891..39e90691 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,33 @@ +//Find Missing Number in a sorted array +//We are taking the approach as the sorted array value would be arr[i]=i+1 +//Time complexity would be O(logn) and space would be O(1) +public class Problem1 { + //Search for 3 here + public static int binarySearch(int arr[]) { + int n= arr.length; + int low=0; + int high=n-1; + while(low<=high) { //initiated binary search + + int mid= low +(high-low)/2; + //right sorting + if(arr[mid]==mid+1) { + low=mid+1; + } + //left sorting + else { + high=mid-1; + } + } + //When the loop ends, low points to the index where the mismatch starts. + return low+1; + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + int arr[] = {1,2,4,5,6}; + int result = binarySearch(arr); + System.out.print(result); + } +} diff --git a/Problem2.java b/Problem2.java index 8b137891..c3fe8ccb 100644 --- a/Problem2.java +++ b/Problem2.java @@ -1 +1,118 @@ +class MyMinHeap { + private int[] heap; + private int size; + private int maxSize; + // Constructor to initialize the heap with a given capacity + public MyMinHeap(int capacity) { + this.maxSize = capacity; + this.size = 0; + heap = new int[capacity]; // Allocate space for the heap + } + + // Returns the index of the parent of the node at index i + private int parent(int i) { + return (i - 1) / 2; + } + + // Returns the index of the left child of the node at index i + private int leftChild(int i) { + return 2 * i + 1; + } + + // Returns the index of the right child of the node at index i + private int rightChild(int i) { + return 2 * i + 2; + } + + // Checks if the node at index i is a leaf node + private boolean isLeaf(int i) { + return i >= size / 2 && i < size; + } + + // Swaps two elements in the heap array + private void swap(int i, int j) { + int temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + + // Maintains the min-heap property by pushing down the element at index i + private void heapify(int i) { + if (isLeaf(i)) return; // No need to heapify leaf nodes + + int left = leftChild(i); + int right = rightChild(i); + int smallest = i; + + // Find the smallest among current, left, and right + if (left < size && heap[left] < heap[smallest]) { + smallest = left; + } + if (right < size && heap[right] < heap[smallest]) { + smallest = right; + } + + // If the smallest is not the current node, swap and continue heapifying + if (smallest != i) { + swap(i, smallest); + heapify(smallest); + } + } + + public void insert(int val) { + if (size >= maxSize) return; // Heap is full + + heap[size] = val; // Place the new value at the end + int current = size; + size++; + + // Bubble up the new value to maintain heap property + while (current > 0 && heap[current] < heap[parent(current)]) { + swap(current, parent(current)); + current = parent(current); + } + } + + public int removeMin() { + if (size == 0) return -1; // Heap is empty + + int min = heap[0]; + heap[0] = heap[size - 1]; // Replace root with the last element + size--; // Reduce heap size + heapify(0); // Restore heap property from the root + 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); + + // Display the heap structure + System.out.println("Min Heap:"); + heap.printHeap(); + + // Remove and print the minimum element + System.out.println("Removed Min: " + heap.removeMin()); + } +} \ No newline at end of file