Pages

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.`);

Thursday, October 30, 2025

Connect n ropes with minimum cost in Java ( solved )

 import java.util.*;


public class Main {

    

    public static int minRopeCost(int[] arr, int n) {

        PriorityQueue<Integer> pq = new PriorityQueue<Integer>();

        

        

        // add all elements to the min-heap

        for(int i=0; i<n; i++) {

            pq.add(arr[i]);

        }

        

        int res = 0;

        

        // keep combining two smallest el.

        while(pq.size() > 1) 

        {

            int first = pq.poll(); // 1st smallest elements 

            int second = pq.poll(); // 2nd smallest elements

            

            res += first+second; // add their combined cost;

            

            int new_rope = first + second; // store the new rope

            

            pq.add(new_rope); // push the new rope back into the heap

        }

        

        // return total cost

        return res;

        

    }

    

    

    

    

public static void main(String[] args) {

int[] rope = {3, 2, 4, 6};

int n = rope.length;

System.out.println("Total cost for connecting ropes is " + minRopeCost(rope, n));

}

}


problems i faced:

1. Main.java:43: error: reached end of file while parsing } ^ 1 error

FIX: 
Make sure main , class and function braces are closed properly.

Wednesday, October 29, 2025

Self-practice-01: Check if arrays are matching or not in Java

 import java.util.*;


public class Main

{

    

    

    public static void ifMatch(int[] arr) {

        

        // Combine array with a predefined array

        int[] pattern = {4, 3, 2, 6};

        

        // Check if arrays have same length

        if(Arrays.equals(arr, pattern)) {

            System.out.println("Yes, Array is matching.");

        } else {

            System.out.println("No, Array isn't matching.");

        }

    }

    

    

    

public static void main(String[] args) {

int[] testArr = {4, 3, 2, 6};

System.out.print("Array 1: ");

ifMatch(testArr);

int[] testArr2 = {4, 3, 2, 9};

System.out.print("Array 2: ");

ifMatch(testArr2);

}

}

Contains duplicate - using HashSet (optimized code)

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