Pages

Wednesday, March 18, 2026

3Sum - Leetcode solution - How i turned into two-pointer approach?

 class Solution {

    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums); // first sort -> helps to turn into 2pointer
        List<List<Integer>> res = new ArrayList<>();

        for(int i=0; i<nums.length-2; i++) {
           
            // skip duplicates
            if(i > 0 && nums[i] == nums[i-1]) continue;

            int left = i+1;
            int right = nums.length-1;

            while(left < right) {

                int sum = nums[i] + nums[left] + nums[right];

                if(sum == 0) {
                    // add
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    // skip duplicates
                    while(left < right && nums[left] == nums[left+1]) left++;
                    while(left < right && nums[right] == nums[right-1]) right--;
                    left++; right--;

                }
                // move pointers
                else if(sum < 0){
                     left++;
                     }
                else {
                    right--;
                    }
            }

        }

        return res;


       
    }
}

167. Two Sum II - Input Array Is Sorted

1. TWO POINTER APPROACH


 class Solution {

    public int[] twoSum(int[] numbers, int target) {
        // 1. two pointer approach - initialize the var.
        int left = 0;
        int right = numbers.length-1;

        // 2. while loop until pointers meet
        while(left < right) {
            int currentSum = numbers[left]+numbers[right];

            // 3. check the sum
            if(currentSum == target) {
                return new int[] {left+1, right+1}; // why we did this, Q. asked 1-indexed array, so we add +1 in our 0-idxed array
            } if(currentSum < target) {
                left++;
            } else {
                right--;
            }
        }
        return new int[] {-1, -1}; // -1 cuz exactly 1 solution
    }

   
}


2.


1. TWO POIN1. TWO POINTER APPROACHTER APPROACH

Friday, March 13, 2026

Next Greater element 1 - monotonic stack problem - LEETCODE 496

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap();
        Deque<Integer> stack = new ArrayDeque<>(); // faster than stack

        for(int num : nums2) { // scans each no. in nums2
           
            while(!stack.isEmpty() && stack.peek() < num) {
                map.put(stack.pop(), num);
            }
            stack.push(num);
           
        }

        int[] result = new int[nums1.length]; // num1 jitna length ka answer hona chaiye!
       
        for(int i=0; i<nums1.length; i++) {
            result[i] = map.getOrDefault(nums1[i], -1); // if it exists, return value nums1[i] else return -1
        }

        return result;
       
           
        }
    }

NEW TO TECH WORLD: natural progression path: when to choose backend, sre, platform engieering? clear all your buzzwords doubts here!

🤔 Feeling Confused? Good. That’s normal. The tech world throws around a lot of buzzwords. So instead of thinking: “Which role should I...