diff --git a/pointers.java b/pointers.java new file mode 100644 index 00000000..1e022755 --- /dev/null +++ b/pointers.java @@ -0,0 +1,104 @@ + +// Approach: +// 1. Count the number of 0s, 1s, and 2s in the array. +// 2. Traverse the array again and place all 0s first. +// 3. Fill the next positions with 1s. +// 4. Fill the remaining positions with 2s. +// 5. This sorts the array using the counting technique. + +// Time Complexity: O(n) +// Space Complexity: O(1) +class Solution { + public void sortColors(int[] nums) { + int n=nums.length; + int count1=0; + int count2=0; + int count3=0; + for(int i=0;i> threeSum(int[] nums) { + Arrays.sort(nums); + List> res=new ArrayList<>(); + int n=nums.length; + for(int i=0;i0){ + right--; + }else{ + left++; + } + } + } + return res; + } +} +//problem 3 +// Approach: +// 1. Place one pointer at the beginning and another at the end of the array. +// 2. Calculate the area formed by the two lines. +// 3. Update the maximum area if the current area is larger. +// 4. Move the pointer with the smaller height inward since it limits the area. +// 5. Repeat until the two pointers meet. + +// Time Complexity: O(n) +// Space Complexity: O(1) +class Solution { + public int maxArea(int[] height) { + int n=height.length; + int left=0; + int right=n-1; + int ans=0; + while(left