From 51c169e45eb13c2c29fafdcf50ff556d2b25e0e8 Mon Sep 17 00:00:00 2001 From: Denishm39 Date: Tue, 24 Feb 2026 12:40:22 -0800 Subject: [PATCH] Problem solutions. --- Problem1.java | 82 ++++++++++++++++++++++++++++++++++++++ Problem2.java | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.java | 77 ++++++++++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java create mode 100644 Problem3.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..5b6b93b6 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,82 @@ +//This uses the Dutch National Flag Algorithm. + +//We maintain 3 pointers: +//low → position for next 0 +//mid → current element +//high → position for next 2 +//Rules: +//if 0 → swap with low, move low and mid +//if 1 → move mid +//if 2 → swap with high, move high only +// +//Time: O(n) +//Space: O(1) + +class Solution { + + public void sortColors(int[] nums) { + + // Pointer for next position of 0 + int low = 0; + + // Pointer for next position of 2 + int high = nums.length - 1; + + // Current pointer to traverse array + int mid = 0; + + + // Traverse until mid crosses high + while(mid <= high) { + + + // Case 1: If element is 2 + if(nums[mid] == 2) { + + // Swap current element with high pointer + swap(nums, mid, high); + + // Decrease high pointer + // because correct position of 2 is at end + high--; + + // DO NOT increase mid here + // because swapped element needs to be checked + } + + + // Case 2: If element is 0 + else if(nums[mid] == 0) { + + // Swap current element with low pointer + swap(nums, mid, low); + + // Increase low pointer + low++; + + // Increase mid pointer + mid++; + } + + + // Case 3: If element is 1 + else { + + // 1 is already in correct middle position + mid++; + } + } + } + + + + // Swap function to exchange two elements + private void swap(int[] nums, int x, int y) { + + int temp = nums[x]; + + nums[x] = nums[y]; + + nums[y] = temp; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..323d377c --- /dev/null +++ b/Problem2.java @@ -0,0 +1,107 @@ +//We use Sorting + Two Pointer technique. +// +//Steps: +//Sort the array +//Fix one number (i) +//Use two pointers (low, high) to find remaining two numbers +//Skip duplicates to avoid duplicate triplets +//Move pointers based on sum +// +//Time: O(n²) +//Space: O(1) (excluding result) + +class Solution { + + public List> threeSum(int[] nums) { + + + // Step 1: Sort the array + // Sorting helps use two pointer technique and skip duplicates + Arrays.sort(nums); + + + int n = nums.length; + + + // Result list to store all unique triplets + List> result = new ArrayList<>(); + + + // Step 2: Fix first element + for(int i = 0; i < n; i++) { + + + // Skip duplicate values for first element + // to avoid duplicate triplets + if(i != 0 && nums[i] == nums[i - 1]) + continue; + + + // If current number > 0, no triplet can sum to 0 + // because array is sorted + if(nums[i] > 0) + break; + + + + // Step 3: Use two pointer for remaining array + int low = i + 1; + int high = n - 1; + + + + while(low < high) { + + + // Calculate current sum + int sum = nums[i] + nums[low] + nums[high]; + + + + // Case 1: Found valid triplet + if(sum == 0) { + + + // Add triplet to result + result.add(Arrays.asList(nums[i], nums[low], nums[high])); + + + // Move both pointers + low++; + high--; + + + // Skip duplicate values for low pointer + while(low < high && nums[low] == nums[low - 1]) + low++; + + + // Skip duplicate values for high pointer + while(low < high && nums[high] == nums[high + 1]) + high--; + } + + + + // Case 2: Sum is too large → decrease high + else if(sum > 0) { + + high--; + } + + + + // Case 3: Sum is too small → increase low + else { + + low++; + } + } + } + + + + // Return all unique triplets + return result; + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..6125ce08 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,77 @@ +//We use Two Pointer technique. +// +//Key idea: +//Area = height × width +//Width = high - low +//Height = minimum of two heights +//To maximize area: +//Move the pointer with smaller height +//Because moving taller height cannot increase area +// +//Time: O(n) +//Space: O(1) + +class Solution { + + public int maxArea(int[] height) { + + + // Length of array + int n = height.length; + + + // Two pointers: start and end + int low = 0; + int high = n - 1; + + + // Variable to store maximum area + int area = 0; + + + + // Traverse until pointers meet + while(low < high) { + + + // Width between two lines + int w = high - low; + + + // Variable to store minimum height + int h = 0; + + + + // Move pointer with smaller height + if(height[low] < height[high]) { + + // Height is limited by smaller line + h = height[low]; + + // Move left pointer right + low++; + } + + + else { + + // Height is limited by smaller line + h = height[high]; + + // Move right pointer left + high--; + } + + + + // Calculate area and update maximum area + area = Math.max(area, h * w); + } + + + + // Return maximum water area + return area; + } +} \ No newline at end of file