Pages

Friday, February 6, 2026

1480. Running Sum of 1d Array - leetcode solution

 class Solution {

    public int[] runningSum(int[] nums) {
        for(int idx = 1; idx<nums.length; idx++) {
            nums[idx] = nums[idx-1] + nums[idx];

        }

        return nums;
    }
}

Wednesday, December 17, 2025

remove duplicates from sorted array - two pointer approach (leetcode)

 class Solution {

    public int removeDuplicates(int[] nums) {
        // base case: return if array have no el.
        if(nums.length == 0) return 0;

        int k = 1; // writer pointer , index = 0 is unique already, so start at 1

        // i - reader pointer
        for(int i=1; i<nums.length; i++) {
            //LOGIC: check if curr. no. is diff than prev. no.
            if(nums[i] != nums[i-1]) {
                // diff? -> unique no. -> store
                nums[k] = nums[i];

                // same? -> not unique. -> dont do anything
                // keep incrementing k
                k++;
               
            }
        }
        return k;
    }
}

Sunday, November 30, 2025

Contains duplicate - using HashSet (optimized code)

 class Solution {

    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> seen = new HashSet<>();

        for(int i=0; i<nums.length; i++) {
            if(seen.contains(nums[i])) { // methods: add(), contains(), remove()
                return true;
            } else {
                seen.add(nums[i]);
            }
        }
        return false;
    }
}

Two Sum - optimized code (using HashMaps)

 public int[] twoSum(int[] nums, int target) {

    // Phase 2 Logic: HashMap

    HashMap<Integer, Integer> map = new HashMap<>();


    for (int i = 0; i < nums.length; i++) {

        int complement = target - nums[i];


        if (map.containsKey(complement)) {

            // Success: Return [Old Index, Current Index]

            return new int[] { map.get(complement), i };

        }

        

        // Store: Key = Number, Value = Index

        map.put(nums[i], i);

    }

    return new int[] {}; // Fallback

}

Tuesday, November 11, 2025

Two Sum in Leetcode

 class Solution {

    public int[] twoSum(int[] nums, int target) {
        for(int i=0; i<nums.length; i++) {
            for(int j=i+1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{};
    }
}

Hash Map in Java!

 HashMap: Part of JCF & implements the Map interface.

- Stores element in key-value pairs.
- Keys are unique and values can be duplicated.

Monday, November 10, 2025

Template Literal in JavaScript

 1. Calculate total price using template literal.
let pencilPrice = 10;

let eraserPrice = 5;
// let output = "The total price is : " + (pencilPrice + eraserPrice) + " Rupees. ";
console.log(`The total price is : ${pencilPrice + eraserPrice} Rupees.`);

1480. Running Sum of 1d Array - leetcode solution

  class Solution {     public int [] runningSum ( int [] nums ) {         for ( int idx = 1 ; idx< nums . length ; idx++) {         ...