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
32 changes: 32 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public void sortColors(int[] nums) {
int n= nums.length;
int left=0;
int mid=0;
int right=n-1;
while(mid<=right){
//move 2 towards right side using mid pointer
if(nums[mid]==2){
swapColors(nums, mid, right);
right--;
}
//move 0 towards left side using mid pointer
else if(nums[mid]==0){
swapColors(nums, mid, left);
mid++; left++;
}
else{ // for nums[mid]==1, contains 1's in middle
mid++;
}

}

}

private void swapColors(int[] nums, int mid, int idx){
int temp=nums[idx];
nums[idx]=nums[mid];
nums[mid]=temp;

}
}
38 changes: 38 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n= nums.length;
if(nums== null || nums.length<3) return new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<n;i++){
if(nums[i]>0) break;
//for external duplicacy
if(i>0 && nums[i]==nums[i-1]) continue;
int l=i+1; int r=n-1;
while(l<r){
int sum= nums[i]+nums[l]+nums[r];
if(sum==0){
//get the triplet
List<Integer> li= Arrays.asList(nums[i],nums[l],nums[r]);
result.add(li);
l++;
r--;
//for internal duplicacy check
while(l<r && nums[l]==nums[l-1]) l++;
while(l<r && nums[r]==nums[r+1]) r--;

}
else if(sum>0){
r--;
}
else{
l++;
}
}
}
return result;

}
}
21 changes: 21 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int maxArea(int[] height) {
int n=height.length;
int max=0;
int l=0; //where l is left pointer
int r=n-1; //where r is right pointer
while(l<r){
int currArea= Math.min(height[l],height[r])*(r-l);
max=Math.max(max,currArea);
if(height[l]<height[r]){
//if height of right is greater than move the left pointer to next index
l++;
}
else{
//if hieght of left is grater than right then move the right pointer to i-1
r--;
}
}
return max;
}
}