From 997c8433ab0a6a1ede1e9a0926b34c2066496e19 Mon Sep 17 00:00:00 2001 From: Praniksha123 Date: Thu, 2 Jul 2026 21:11:59 +0530 Subject: [PATCH] Create pointers.java --- pointers.java | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 pointers.java 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