diff --git a/3sum.cs b/3sum.cs new file mode 100644 index 00000000..6e8be57d --- /dev/null +++ b/3sum.cs @@ -0,0 +1,99 @@ +public class Solution1 { + //Approach + //Sort the Array to use 2 pointer + //Iterate over an array , pick one element and apply 2 pointer using left and right on rest of the array + // skip the duplicates to avoid duplicate sum + public IList> ThreeSum(int[] nums) + { + // Time: O(n²) + // Space: O(1) + + List> result = new(); + if (nums.Length == 0 || nums.Length < 3) + { + return result; + } + Array.Sort(nums); + for (int i = 0; i < nums.Length; i++) + { + if (i > 0 && nums[i] == nums[i - 1]) + continue; + if (nums[i] > 0) + break; + int l = i + 1; int r = nums.Length - 1; + + while (l < r) + { + int sum = nums[i] + nums[l] + nums[r]; + + if (sum == 0) + { + result.Add(new List { nums[i], nums[l], nums[r] }); + l++; r--; + while (l < r && nums[l] == nums[l - 1]) + l++; + while (l < r && nums[r] == nums[r + 1]) + r--; + } + else if (sum < 0) + { + l++; + } + else + { + r--; + } + } + } + return result; + } + +} + +// Using HashSet +// Time: O(n²) +// Space: O(n) + public class Solution2 { + public IList> ThreeSum(int[] nums) + { + List> result = new(); + HashSet<(int,int,int)> hash = new(); + Array.Sort(nums); + if(nums.Length == 0 || nums.Length < 3) + { + return new List>(); + } + for(int i=0;i { nums[i], nums[left], nums[right] }); + } + + left++; + right--; + } + else if(sum > 0) + { + right--; + } + else + { + left++; + } + } + } + return result; + } + +} + diff --git a/ContainerWithMostWater.cs b/ContainerWithMostWater.cs new file mode 100644 index 00000000..f20e942c --- /dev/null +++ b/ContainerWithMostWater.cs @@ -0,0 +1,39 @@ + + +// Time Complexity - O(n); +// Space Complexity - O(1); + +// Approach - Two Pointer Solution + +// Start with two pointers at both ends like left , right and keep checking the area between them. +// if left is smaller move away from left pointer and vice a versa. +// Keep track of the maximum area found during this process. +public class Solution +{ + public int MaxArea(int[] height) + { + + int max = 0; int n = height.Length; + int left = 0; int right = n - 1; + + while (left < right) + { + int min = Math.Min(height[left], height[right]); + int width = right - left; + int area = min * width; + max = Math.Max(max, area); + + if (height[left] < height[right]) + { + left++; + } + else + { + right--; + } + } + return max; + } +} + + diff --git a/SortColors.cs b/SortColors.cs new file mode 100644 index 00000000..8652cb02 --- /dev/null +++ b/SortColors.cs @@ -0,0 +1,42 @@ + +// Time Complexity - O(n); +// Space Complexity - O(1); + +// Approach - Two Pointer Solution + +// We move 0s to the front and 2s to the end, while keeping 1s in the middle. +// Use three pointers: left for 0s, right for 2s, and mid to scan the array. +// Swap accordingly and only move mid forward when the current item is 0 or 1. +public class Solution +{ + public void SortColors(int[] nums) + { + int mid = 0; + int left = 0; + int right = nums.Length - 1; + while (mid <= right) + { + if (nums[mid] == 2) + { + Swap(nums, mid, right); + right--; + } + else if (nums[mid] == 0) + { + Swap(nums, left, mid); + left++; + mid++; + } + else + { + mid++; + } + } + } + private void Swap(int[] nums, int left, int right) + { + int temp = nums[left]; + nums[left] = nums[right]; + nums[right] = temp; + } +} \ No newline at end of file