Posts

Divisible Drama — Who’s In, Who’s Out?

 Divisible Drama — Who’s In, Who’s Out? Problem Statement You’re given two positive integers n and m . num1 = sum of all numbers from 1 to n not divisible by m num2 = sum of all numbers from 1 to n divisible by m Your mission, should you choose to accept it: Return num1 - num2 Example Input: n = 10 , m = 3 Output: 19 Breakdown: Not divisible by 3 → [ 1 , 2 , 4 , 5 , 7 , 8 , 10 ] → num1 = 37 Divisible by 3 → [ 3 , 6 , 9 ] → num2 = 18 Answer = 37 - 18 = 19 Best Data Structure for Solving the Problem This one’s a math.  Why overthink it when Gauss already gave us the formula for sum of 1 to n? Different Approaches (from Brute Force to Optimized) Approach 1: Brute Force (aka List & Sum Vibes) Loop from 1 to n , split the nums based on whether they’re divisible by m , and sum them up. class Solution { public int differenceOfSums ( int n, int m) { int num1 = 0 , num2 = 0 ; for ( int i = 1 ; i <= n; i++)...

Pascal's Triangle

 Pascal Vibes — Building Triangles Like a Pro Problem Statement Given an integer numRows , return the first numRows of Pascal's Triangle . Each number in the triangle is the sum of the two numbers directly above it . Example: Input: numRows = 5 Output: [ [ 1 ], [ 1,1 ], [ 1,2,1 ], [ 1,3,3,1 ], [ 1,4,6,4,1 ] ] Best Data Structure for Solving the Problem This is a CLASSIC example of Tabulation-based Dynamic Programming   We're filling a triangle row-by-row where: triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j] So we’ll use: A 2D List<List<Integer>> (or array of arrays) No need for fancy trees, heaps, or graphs here. Just pure math and memory  Different Approaches (from Brute Force to Optimized) Approach 1: Build Row by Row Using Previous Row This is the “OG” method — intuitive and easy to visualize. Start with [1] For each new row: Add 1 at the start and end In-between values are sum of two values above it class ...

XOR to the Max

Tree Vibees - XORing to the Max Problem Statement You’re given a tree with n nodes numbered from 0 to n - 1 , and a 0-indexed array nums where nums[i] is the value of the i-th node. You’re also given a positive integer k . You can perform the following operation any number of times: Pick an edge [u, v] and set: nums[u] = nums[u] ^ k nums[v] = nums[v] ^ k Return the maximum possible sum of all nums[i] after performing any number of such operations. Example: Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] Output: 6 Explanation: Choose edge [0,2]: nums becomes [2,2,2] => sum = 6 Best Data Structure for Solving the Problem This is a classic greedy XOR game on a tree. Since any edge can be used, and operations are reversible, the tree structure becomes less important here. So no need to traverse the tree! We mainly need: Arrays and bitwise operations. Optional but interview-valuable: Dynamic Programming on Trees (for variations). Multiple Approaches ...

Needle-Haystack

Find the First Occurrence of Needle in Haystack Problem Statement Implement a function strStr(haystack, needle) that returns the index of the first occurrence of needle in haystack , or -1 if needle is not part of haystack . Example : Input: haystack = "sadbutsad" , needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6 . The first occurrence is at 0 . Best Data Structure for Solving the Problem Since we're dealing with string pattern matching, a simple String traversal with two-pointer or substring comparison is efficient here. Multiple Approaches Approach 1: Built-in Method (Quick & Easy) Idea : Use Java's built-in indexOf() function. It's optimized under the hood and handles all edge cases. public int strStr (String haystack, String needle) { return haystack.indexOf(needle); } Approach 2: Manual Sliding Window (Interview-Ready) Idea : Slide a window of size needle.length() across haysta...

Atoi Problem

String to Integer (atoi)  Problem Statement Implement the myAtoi(string s) function that converts a string into a 32-bit signed integer ( int ) using the following rules: Parsing Rules Whitespace : Skip all leading whitespace characters. Sign : Check for an optional '+' or '-' sign. Digits : Convert digits until a non-digit is found. Clamp : If the number exceeds 32-bit range: [-2^31, 2^31 - 1] = [-2147483648, 2147483647] return the clamped value. Return : The resulting integer. Example  Input: s = " -042" Output: -42 Multiple Approaches Approach 1: Brute Force with Built-in Functions (Not Allowed in Interviews) Idea : Use built-in parsing and catching exceptions. public int myAtoi (String s) { s = s.trim(); try { return Integer.parseInt(s.split( "[^0-9+-]" )[ 0 ]); } catch (Exception e) { return 0 ; } } Approach 2: Manual Parsing with Overflow Detection (Optimal) Idea : Process ...

Zero Array After Queries

Zero Array After Queries You are given an integer array nums and a list of queries, where each query is a pair [li, ri] representing a range. Each query allows you to decrement any subset of elements in the range [li, ri] by 1. Your task is to determine if it is possible to make every element in nums equal to zero after applying all queries in order . Example Input: nums = [ 1 , 0 , 1 ] queries = [[0, 2]] Output: true Explanation: We can select indices 0 and 2 and decrement both by 1 to obtain [0, 0, 0] . Best Data Structure for the Job We are working with multiple range updates and need to evaluate coverage over indices. The best choice here is the difference array combined with a prefix sum . It allows efficient range additions and helps track how many times each index is affected by the queries. Different Approaches Approach 1: Brute Force This naive method iterates over every index for every query and performs the decrement operations directly. class Solution {...