Pages

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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

}

}

Friday, October 10, 2025

Circular Queue using Arrays

 public class QueueB {

    static class Queue {
        static int arr[];
        static int size;
        static int rear;
        static int front;

        Queue(int n) {
            arr = new int[n];
            size = n;
            rear = -1;
            front = -1;
        }

        public boolean isEmpty() {
            return rear == -1 && front == -1;
        }

        public boolean isFull() {
            return (rear + 1) % size == front;
        }

        // add
        public void add(int data) {
            if (isFull()) {
                System.out.println("Queue is full");
                return;
            }

            // add 1st element
            if (front == -1) {
                front = 0;
            }
            rear = (rear + 1) % size;
            arr[rear] = data;
        }

        // remove
        public int remove() {
            if (isEmpty()) {
                System.out.println("Queue is empty");
                return -1;
            }

            int result = arr[front];
            // last element deleted
            if (rear == front) {
                rear = front = -1;
            } else {
                front = (front + 1) % size;
            }
            return result;
        }

        // peek
        public int peek() {
            if (isEmpty()) {
                System.out.println("Queue is empty");
                return -1;
            }
            return arr[front];
        }
    }

    public static void main(String args[]) {
        Queue q = new Queue(3);
        q.add(1);
        q.add(2);
        q.add(3);
        // adding condition that works like a circular queue
        System.out.println(q.remove()); // prints 1
        q.add(4);
        System.out.println(q.remove()); // prints 2
        q.add(5);
        // queue now contains 3,4,5 in order

        while (!q.isEmpty()) {
            System.out.println(q.peek());
            q.remove();
        }
    }
} OUTPUT: 1 2
3
4
5

Sunday, September 14, 2025

Delete N Nodes After M Nodes in a Linked List (Leetcode Premium Problem)

 

Delete N Nodes After M Nodes in a Linked List

Difficulty Level: Rookie

In this problem, you are given a linked list and two integers, M and N. You need to traverse the linked list such that you:

  1. Retain M nodes, then

  2. Delete the next N nodes, and

  3. Repeat this process until the end of the linked list.


Problem Statement

  • Input: A linked list and two integers M and N

  • Output: The modified linked list after deleting N nodes after every M nodes

Saturday, September 13, 2025

Intersection of Two Linked List (Leetcode-160)

 /**

 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // if either list are null, return null since no intersection
        if(headA == null || headB == null) {
            return null;
        }

        // set up two runners
        ListNode pA = headA;
        ListNode pB = headB;

        while(pA != pB) {
            pA = (pA == null) ? headB : pA.next;
            pB = (pB == null) ? headA : pB.next;
        }
        return pA;
    }
} OUTPUT: All cases are true.

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...