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
82 changes: 82 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
107 changes: 107 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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<List<Integer>> 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;
}
}
77 changes: 77 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -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;
}
}