From 4b3fc52fa22465ffd76745a88a0150cd670eff3d Mon Sep 17 00:00:00 2001 From: Samiksha Manjunath Date: Sat, 16 May 2026 17:21:56 -0700 Subject: [PATCH 1/2] Done Competitive-Coding- --- Problem1.java | 23 ++++++++++ Problem2.java | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/Problem1.java b/Problem1.java index 8b137891..9e419b63 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,24 @@ +// Time Complexity : O(n) because we traverse the array once and HashMap operations take O(1) on average +// Space Complexity : O(n) for storing numbers and their indices in the HashMap +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Your code here along with comments explaining your approach in three sentences only +// We use a HashMap to store each number along with its index as we iterate through the array. +// For every number, we calculate the required complement needed to reach the target and check whether it already exists in the map. +// If the complement exists, we return the stored index and current index as the required pair; otherwise, we store the current number and continue. + +class Solution { + public int[] twoSum(int[] nums, int target) { + Map hashMap = new HashMap<>(); + for (int i=0; i O(log n), pop -> O(log n), peek -> O(1), size -> O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Your code here along with comments explaining your approach in three sentences only +// We implement a Min Heap using an ArrayList where the smallest element is always stored at the root. +// push inserts the element at the end and bubbles it upward to maintain heap order, while pop removes the root and heapifies downward. +// peek returns the minimum element at the root, and size returns the total number of elements currently present in the heap. + +class minHeap { + + List heap; + + // Constructor + public minHeap() { + + // initialize heap + heap = new ArrayList<>(); + } + + public void push(int x) { + + // insert element at end + heap.add(x); + + int curr = heap.size() - 1; + + // bubble up + while (curr > 0) { + + int parent = (curr - 1) / 2; + + // maintain min heap property + if (heap.get(parent) > heap.get(curr)) { + + swap(parent, curr); + + curr = parent; + } else { + break; + } + } + } + + public void pop() { + + // heap empty + if (heap.size() == 0) { + return; + } + + int last = heap.get(heap.size() - 1); + + // move last element to root + heap.set(0, last); + + // remove last element + heap.remove(heap.size() - 1); + + // restore heap property + heapify(0); + } + + public int peek() { + + // return minimum element + if (heap.size() == 0) { + return -1; + } + + return heap.get(0); + } + + public int size() { + + // return heap size + return heap.size(); + } + + // heapify downward + private void heapify(int i) { + + int smallest = i; + + int left = 2 * i + 1; + + int right = 2 * i + 2; + + // compare left child + if (left < heap.size() && + heap.get(left) < heap.get(smallest)) { + + smallest = left; + } + + // compare right child + if (right < heap.size() && + heap.get(right) < heap.get(smallest)) { + + smallest = right; + } + + // swap if needed + if (smallest != i) { + + swap(i, smallest); + + heapify(smallest); + } + } + + // helper swap function + private void swap(int i, int j) { + + int temp = heap.get(i); + + heap.set(i, heap.get(j)); + + heap.set(j, temp); + } +} From 3b8ffa970aca7704572c5cf5919f10f8ca17b75d Mon Sep 17 00:00:00 2001 From: Samiksha Manjunath Date: Sat, 16 May 2026 17:34:58 -0700 Subject: [PATCH 2/2] Done Competitive-Coding-1 Rev2 --- Problem1.java | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/Problem1.java b/Problem1.java index 9e419b63..995013f7 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1,24 +1,37 @@ -// Time Complexity : O(n) because we traverse the array once and HashMap operations take O(1) on average -// Space Complexity : O(n) for storing numbers and their indices in the HashMap +// Time Complexity : O(log n) because we use Binary Search on the sorted array +// Space Complexity : O(1) // Did this code successfully run on Leetcode : Yes // Any problem you faced while coding this : No // Your code here along with comments explaining your approach in three sentences only -// We use a HashMap to store each number along with its index as we iterate through the array. -// For every number, we calculate the required complement needed to reach the target and check whether it already exists in the map. -// If the complement exists, we return the stored index and current index as the required pair; otherwise, we store the current number and continue. +// We use Binary Search to find the first index where the expected value and actual value do not match. +// Before the missing number, nums[i] == i + 1 holds true, and after the missing number this relationship breaks. +// The first mismatching position helps us determine the missing number directly. class Solution { - public int[] twoSum(int[] nums, int target) { - Map hashMap = new HashMap<>(); - for (int i=0; i