Recursive indexing involves creating a chain of elements by repeatedly using values as indices. Given a list A and starting index k, we build a set {A[k], A[A[k]], A[A[A[k]]], ...} until we reach an out-of-bounds index or detect a cycle. If there's a cycle (we encounter the same value twice), we return -1. Otherwise, we return the size of the unique elements collected. Example Walkthrough For A = [1, 2, 3, 4, 5, 6, 7] and k = 1: A[1] = 2, add 2 to set A[2] = 3, add 3 to set A[3] = ... Read More
In string processing, we often need to find the first recurring character — the first character that appears again in the string. This problem requires us to track which characters we've seen and return the index where we encounter a character for the second time. So, if the input is like "abcade", then the output will be 3, as 'a' appears first at index 0 and recurs at index 3. Approach To solve this, we will follow these steps ? Create a set to store characters we've already seen ... Read More
Suppose we have a rectangle that is represented as a list with four elements [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Two rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap. So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True. Visual Representation R1 R2 Overlap X-axis Y-axis 0 1 2 3 0 1 2 Algorithm To solve this, we will follow these steps − if R1[0]>=R2[2] or R1[2]
A Pythagorean triplet is a set of three numbers a, b, and c such that a² + b² = c². Given a list of numbers, we need to check whether there exist three numbers that form a Pythagorean triplet. So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8² + 6² = 64 + 36 = 100 = 10². Algorithm To solve this problem efficiently, we will follow these steps − Create a list of squares of all numbers in descending order ... Read More
Prime factorization is the process of breaking down a number into its prime factors. A prime factor is a prime number that divides the given number exactly. For example, the prime factors of 42 are 2, 3, and 7 because 42 = 2 × 3 × 7. So, if the input is 42, then the output will be [2, 3, 7]. Algorithm To find all prime factors efficiently, we follow these steps: Create an empty result list Handle factor 2 separately (divide by 2 until odd) Check odd numbers from 3 to √n If any ... Read More
Suppose we have two lists of numbers called nums and costs. We can increase or decrease nums[i] for cost costs[i]. We want to make all elements in nums equal with minimum total cost. So, if the input is like nums = [3, 2, 4] and costs = [1, 10, 2], then the output will be 5. We can decrease 3 to 2 for cost 1, then decrease 4 to 2 (twice) for cost 2 each, totaling 1 + 2 + 2 = 5. Approach This problem uses ternary search to find the optimal target value. The cost ... Read More
A binary tree can be transformed by updating each node's value to include the sum of all values in its left and right subtrees. This creates a new tree where each node contains its original value plus the total sum of all descendant nodes. .node-circle { fill: #e8f4f8; stroke: #2c5aa0; stroke-width: 2; } .node-text { font-family: Arial; font-size: 12px; text-anchor: middle; dominant-baseline: central; } .tree-line { stroke: #333; stroke-width: 2; } ... Read More
When working with palindromes, we need to understand that a palindrome reads the same forwards and backwards. To count unique palindromes from string characters, we use combinatorial mathematics to calculate arrangements of the first half of the palindrome. Algorithm Overview The key insight is that for a palindrome to be possible, at most one character can have an odd frequency (this becomes the middle character). We then calculate arrangements of the first half using factorial division ? Step-by-Step Approach Count frequency of each character in the string Check if palindrome formation is possible (at most ... Read More
A vertex-to-vertex reachability matrix is a 2D matrix that shows whether there is a path between any two vertices in a graph. For a graph with n vertices, we create an n×n matrix M where: M[i, j] = 1 when there is a path between vertices i and j M[i, j] = 0 otherwise Problem Example Given a graph represented as an adjacency list, we need to find the reachability matrix. Consider this graph: 0 1 ... Read More
Sometimes we need to find the number of ways to throw n dice with a specific number of faces to achieve a target total. This is a classic dynamic programming problem that can be solved efficiently using bottom-up approach. Problem Statement Given n dice, each with a specific number of faces, find how many ways we can throw them to get a target total. Since the answer can be very large, return the result modulo 10^9 + 7. For example, if n = 2, faces = 6, and total = 8, there are 5 ways: (2, 6), ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance