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..53d1051d 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,38 @@ +// https://www.geeksforgeeks.org/find-the-missing-number-in-a-sorted-array/ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Binary search approach: calculate the mid index and check if the difference between +// the value at mid and mid is same as the difference between low and low index. +// If not, then the missing number is in the left half, else it is in the right half. +// Repeat this process until we find the missing number. +// the final answer will be the value at low index + 1. + +class Main { + static int missingNumber(int[] arr) { + int n = arr.length; + + if (arr[0] != 1) { + return 1; + } + if (arr[n - 1] != (n + 1)) { + return n + 1; + } + + int low = 0, high = n - 1; + while (high - low > 1) { + int mid = low + (high-low) / 2; + if ((arr[low] - low) != (arr[mid] - mid)) { + high = mid; + } else if ((arr[high] - high) != (arr[mid] - mid)) { + low = mid; + } + } + return (arr[low] + 1); + } + + public static void main(String[] args) { + int[] arr = {1, 2, 3, 5, 6, 7, 8}; + System.out.println(missingNumber(arr)); + } +} \ 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..a9f64499 100644 --- a/Problem2.java +++ b/Problem2.java @@ -1 +1,112 @@ +// https://www.geeksforgeeks.org/min-heap-in-java/ +// Time Complexity : O(log n) for insert and removeMin operations, +// O(n) for printHeap, +// O(1) for parent, leftChild, rightChild, isLeaf, and swap operations. +// Space Complexity : O(n) for the heap array. +class MyMinHeap { + private int[] heap; + private int size; + private int maxSize; + + public MyMinHeap(int capacity) { + this.maxSize = capacity; + this.size = 0; + 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 left = leftChild(i); + int right = rightChild(i); + int smallest = 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); + heapify(smallest); + } + } + + public void insert(int val) { + if (size >= maxSize) return; + + heap[size] = val; + int current = size; + size++; + + while (current > 0 && heap[current] < heap[parent(current)]) { + swap(current, parent(current)); + current = parent(current); + } + } + + 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()); + } +}