From eeb02c6058e004443a08804680b5ae82a7000f9f Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Mon, 29 Dec 2025 15:53:00 -0500 Subject: [PATCH] Completed Competitive Coding-1 --- Problem1.java | 1 - Problem2.java | 1 - src/MinHeap.java | 100 ++++++++++++++++++++++++++++++++ src/MissingElement.java | 22 +++++++ src/TestCompetitiveCoding1.java | 19 ++++++ 5 files changed, 141 insertions(+), 2 deletions(-) delete mode 100644 Problem1.java delete mode 100644 Problem2.java create mode 100644 src/MinHeap.java create mode 100644 src/MissingElement.java create mode 100644 src/TestCompetitiveCoding1.java 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 @@ - diff --git a/src/MinHeap.java b/src/MinHeap.java new file mode 100644 index 00000000..ddd5567c --- /dev/null +++ b/src/MinHeap.java @@ -0,0 +1,100 @@ +/* +problem statement - Design MinHeap +Time Complexity - +insert - O(log n) inserting a new key takes O(Log n) time. We add a new key at the end of the tree. If a new key is larger than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property. +remove - O(log n) if removing the root element violates the property of heap we have to perform heapify starting from the root to retore heap properties +Space Complexity - O(n) + */ + +public class MinHeap { + private int [] minHeap; + private int size; + private int maxSize; + private static final int FRONT = 1; + + public MinHeap(int maxSize) { + this.maxSize = maxSize; + this.size = 0; + minHeap = new int[this.maxSize+1]; + minHeap[0] = Integer.MIN_VALUE; + } + + // Method to return the position the parent node + private int findParent(int index) { + return index/2; + } + //method to return the position the left node + private int findLeft(int index) { + return (index*2); + } + //method to return the position of right node + + private int findRight(int index) { + return (index*2)+1; + } + //method to find the leaf node + private boolean isLeafNode(int index) { + if(index > (size/2)){ + return true; + } + return false; + } + private void minHeapify(int index){ + while (!isLeafNode(index)){ + int left = findLeft(index); + int right = findRight(index); + int smallest = left; + if(right <= size && minHeap[right] < minHeap[left]){ + smallest = right; + } + if(minHeap[index] <= minHeap[smallest]){ + break; + } + swap(index,smallest); + index = smallest; + } + } + + private void swap(int index1, int index2) { + int temp = minHeap[index1]; + minHeap[index1] = minHeap[index2]; + minHeap[index2] = temp; + } + public void insert(int value) { + if(size >= maxSize){ + return; + } + minHeap[++size] = value; + int current = size; + //swap the current value if the current value is less than its parent + while(minHeap[current] < minHeap[findParent(current)]){ + swap(current, findParent(current)); + current = findParent(current); + } + } + public void print() { + for (int i = 1; i <= size / 2; i++) { + System.out.print(" PARENT : " + minHeap[i]); + + int left = findLeft(i); + int right = findRight(i); + + if (left <= size) { + System.out.print(" LEFT CHILD : " + minHeap[left]); + } + + if (right <= size) { + System.out.print(" RIGHT CHILD : " + minHeap[right]); + } + + System.out.println(); + } + } + public int remove(){ + int value = minHeap[FRONT]; + minHeap[FRONT] = minHeap[size--]; + minHeapify(FRONT); + return value; + } + +} diff --git a/src/MissingElement.java b/src/MissingElement.java new file mode 100644 index 00000000..81546d2e --- /dev/null +++ b/src/MissingElement.java @@ -0,0 +1,22 @@ +/* +problem statement - Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Find the missing integer. +Approach: Using binary search, calculate the mid and check if the mid is in the right place if yes the missing number would be in the right side of mid update low = mid+1 position if not missing would be the left half +Time Complexity - O(log n) +Space Complexity - O(1) + */ + +public class MissingElement { + public static int missingNumber(int[] arr) { + int low = 0; + int high = arr.length - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (arr[mid] == mid + 1) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return low + 1; + } +} diff --git a/src/TestCompetitiveCoding1.java b/src/TestCompetitiveCoding1.java new file mode 100644 index 00000000..3a7b8aa7 --- /dev/null +++ b/src/TestCompetitiveCoding1.java @@ -0,0 +1,19 @@ +public class TestCompetitiveCoding1 { + public static void main(String[] args) { + MinHeap minHeap = new MinHeap(10); + minHeap.insert(5); + minHeap.insert(3); + minHeap.insert(2); + minHeap.insert(1); + minHeap.insert(4); + minHeap.insert(6); + minHeap.insert(7); + minHeap.print(); + System.out.println("The Min val is " + + minHeap.remove()); + minHeap.print(); + // Test for the missing element + int arr[] = {1,2,3,4,6,7,8}; + System.out.println("Missing element in an array is "+MissingElement.missingNumber(arr)); + } +}