Programming Articles

Page 542 of 2547

Find the original matrix when largest element in a row and a column are given in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 314 Views

This problem involves reconstructing an original matrix when you know the largest element in each row and column, plus a binary matrix indicating where positive values were located. The key insight is that at any position where the binary matrix has a 1, the original value must be the minimum of the row maximum and column maximum. Problem Understanding Given: Array A of size N (maximum elements for each row) Array B of size M (maximum elements for each column) Binary matrix of size N×M where 1 indicates a positive value existed, 0 indicates the position was ...

Read More

Find the ordering of tasks from given dependencies in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 528 Views

Finding the ordering of tasks from given dependencies is a classic topological sorting problem. Given n tasks labeled from 0 to n-1 with prerequisite relationships, we need to determine a valid execution order. This problem uses Depth-First Search (DFS) with cycle detection to ensure all dependencies are satisfied. Problem Understanding If we have a prerequisite pair [2, 1], it means task 1 must be completed before task 2. We need to find an order where all prerequisites are satisfied, or return an empty array if it's impossible due to circular dependencies. Algorithm Approach The solution uses ...

Read More

Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 445 Views

Finding the number of sub-arrays in a permutation where the median equals a specific value M is a classic problem that can be solved efficiently using prefix sums and hash maps. The key insight is that for a sub-array to have median M, the number of elements less than M should be at most equal to the number of elements greater than M. We track the balance using a running sum where we subtract 1 for elements less than M and add 1 for elements greater than M. Algorithm Approach We maintain a balance counter and use ...

Read More

Find the number of spectators standing in the stadium at time t in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 191 Views

This problem simulates spectators standing and sitting in a stadium over time. There are n spectators labeled 1 to n, where at most k spectators can stand simultaneously. We need to find how many spectators are standing at time t. Problem Understanding The pattern works as follows: At time t1, the first spectator stands. At time t2, the second spectator stands. ... At time tk, the k-th spectator stands. At time tk + 1, the (k + 1)-th spectator stands and ...

Read More

Find the number of rectangles of size 2x1 which can be placed inside a rectangle of size n x m in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 356 Views

Finding the number of 2x1 rectangles that can be placed inside an n x m rectangle is a classic combinatorial problem. We need to consider different cases based on whether the dimensions are even or odd. Problem Understanding Given a rectangle of size n x m, we want to find the maximum number of non-overlapping 2x1 rectangles that can be placed inside it. The constraints are ? No two small rectangles can overlap Every small rectangle must lie completely inside the larger one Small rectangles can touch the edges of the larger rectangle ...

Read More

Find the number of distinct pairs of vertices which have a distance of exactly k in a tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 358 Views

Finding the number of distinct pairs of vertices with exactly distance k in a tree is a classic dynamic programming problem. We use DFS traversal with memoization to count paths of specific lengths from each node. Problem Understanding Given a tree with n nodes and an integer k, we need to count all distinct pairs of vertices that are exactly k edges apart. For the tree shown below with k = 2: 1 2 3 ...

Read More

Find the number of distinct islands in a 2D matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 642 Views

An island in a 2D binary matrix is a group of connected land cells (represented by 1s) surrounded by water (represented by 0s). Islands are formed by connecting adjacent lands horizontally or vertically. We can solve this problem using Depth-First Search (DFS) to mark visited land cells. Problem Visualization Consider this binary matrix with three distinct islands ? 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 The blue cells form one island, green forms ...

Read More

Find the number of consecutive zero at the end after multiplying n numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 321 Views

When multiplying numbers, trailing zeros are formed by factors of 10, which come from pairs of factors 2 and 5. To find consecutive zeros at the end of a product, we count the factors of 2 and 5 in all numbers and take the minimum count. So, if the input is like [200, 20, 5, 30, 40, 14], then the output will be 6 as 200 × 20 × 5 × 30 × 40 × 14 = 336000000, there are six 0s at the end. Algorithm Approach To solve this problem, we follow these steps: ...

Read More

Find the missing value from the given equation a + b = c in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 706 Views

In Python, we can solve equations of the form a + b = c where one value is missing (represented by ?). This involves parsing the equation string and applying basic arithmetic to find the unknown value. For example, if the input is ? + 4 = 9, the output will be 5. Algorithm To solve this problem, we follow these steps: Remove all spaces from the string and replace + and = with commas Split the string by commas to get individual elements Find the position of the missing value (?) Apply the appropriate ...

Read More

Find the minimum time after which one can exchange notes in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 212 Views

Suppose there are n cashiers exchanging money. At any moment, the i-th cashier has ki people waiting in line. The j-th person in line for the i-th cashier has m[i, j] notes to exchange. We need to find the minimum time required to exchange notes, given that each cashier takes 5 seconds to scan a single note and 15 seconds to complete the exchange process for each customer. Problem Understanding The total time for each cashier consists of ? Scanning time: 5 seconds × total notes for all customers Exchange time: 15 seconds × number of ...

Read More
Showing 5411–5420 of 25,466 articles
« Prev 1 540 541 542 543 544 2547 Next »
Advertisements