Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions 3sum.cs
Original file line number Diff line number Diff line change
@@ -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<IList<int>> ThreeSum(int[] nums)
{
// Time: O(n²)
// Space: O(1)

List<IList<int>> 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<int> { 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<IList<int>> ThreeSum(int[] nums)
{
List<IList<int>> result = new();
HashSet<(int,int,int)> hash = new();
Array.Sort(nums);
if(nums.Length == 0 || nums.Length < 3)
{
return new List<IList<int>>();
}
for(int i=0;i<nums.Length-2;i++)
{
int left = i+1;
int right = nums.Length - 1;
while(left < right)
{
int sum = nums[i]+nums[left]+nums[right];
if(sum == 0)
{
var triplet = (nums[i], nums[left], nums[right]);

if (hash.Add(triplet)) // Adds only if unique
{
result.Add(new List<int> { nums[i], nums[left], nums[right] });
}

left++;
right--;
}
else if(sum > 0)
{
right--;
}
else
{
left++;
}
}
}
return result;
}

}

39 changes: 39 additions & 0 deletions ContainerWithMostWater.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}


42 changes: 42 additions & 0 deletions SortColors.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}