Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to find number of coins we can pick from topleft to bottom-right cell and return in Python

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

This problem involves finding the maximum coins we can collect by making two simultaneous journeys: one from top-left to bottom-right, and another from bottom-right back to top-left. We can only move right or down in the first journey, and left or up in the return journey. Problem Understanding The matrix contains three types of values: 0 for an empty cell 1 for a coin −1 for a wall (impassable) We need to maximize coin collection while ensuring we can complete both journeys. If either path is blocked, we return 0. Example Consider ...

Read More

Program to check person 1 can win the candy game by taking maximum score or not in Python

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

Suppose two players are playing a candy game where several candies are placed in a line. Person 1 is given a list of numbers called nums representing the point value of each candy. On each player's turn, they can pick 1, 2, or 3 candies from the front of the line, delete them from the list, and add their points to their score. The game ends when all candies are taken, and the player with the higher score wins. We need to determine if person 1 can win this game by playing optimally. Example Scenario If the ...

Read More

Program to check whether first player can take more candies than other or not in Python

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

Suppose we have a list of numbers called candies and two persons are racing to collect the most number of candies. This is a turn-based game where person 1 starts first, and in each turn a player can pick candies from either the front or the back of the list. We need to check whether person 1 can collect more candies than person 2. So, if the input is like candies = [1, 4, 3, 8], then the output will be True. Person 1 can take 8 candies in the first round, and regardless of whether person 2 picks ...

Read More

Program to count number of permutations where sum of adjacent pairs are perfect square in Python

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

Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i]. So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]. In both cases: 2+7=9 (perfect square), 7+9=16 (perfect square), and 9+7=16 (perfect square), 7+2=9 (perfect square). Algorithm To ...

Read More

Program to count number of flipping required to make all x before y in Python

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

When we have a string containing only 'x' and 'y' characters, we want to find the minimum number of character flips needed to arrange all 'x' characters before all 'y' characters. This means we need to achieve a pattern like "xxx...yyy". The key insight is that for any partition point in the string, we can calculate the cost by counting 'y' characters on the left (which need to become 'x') and 'x' characters on the right (which need to become 'y'). Algorithm We iterate through each possible partition point and track: y_left: Number of 'y' ...

Read More

Program to check person can reach top-left or bottomright cell avoiding fire or not in Python

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

Suppose we have a 2D matrix with few different values like below − 0 for empty cell 1 for a person 2 for fire 3 for a wall Now assume there is only one person and in each turn the fire expands in all four directions (up, down, left and right) but fire cannot expand through walls. We have to check whether the person can move to either the top-left corner or the bottom-right corner of the matrix. We have ...

Read More

Program to count number of walls required to partition top-left and bottom-right cells in Python

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

In this problem, we need to find the minimum number of walls required to block all paths between the top-left cell (0, 0) and bottom-right cell (R-1, C-1) in a 2D binary matrix. The matrix contains 0s (empty cells) and 1s (existing walls), and we can only move in four directions (up, down, left, right). Problem Understanding Given a matrix where 0 represents empty cells and 1 represents walls, we need to determine the minimum number of additional walls to place so that there's no path from top-left to bottom-right. We cannot place walls on the start and ...

Read More

Program to count number of distinct characters of every substring of a string in Python

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

Given a lowercase string, we need to find the sum of distinct character counts across all possible substrings. A character is considered distinct in a substring if it appears exactly once. The result should be returned modulo 10^9 + 7 for large numbers. Problem Understanding For string s = "xxy", let's examine all substrings ? "x" (index 0): 1 distinct character "x" (index 1): 1 distinct character "y" (index 2): 1 distinct character "xx" (indices 0-1): 0 distinct characters (x appears twice) "xy" (indices 1-2): 2 distinct characters "xxy" (indices 0-2): 1 distinct character (only y ...

Read More

Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python

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

When we need to find the smallest positive integer x that is divisible by all numbers from 1 to k, we're looking for the Least Common Multiple (LCM) of numbers 1 through k. The trailing zeros in this LCM depend on how many times 10 divides it, which is determined by the minimum of powers of 2 and 5 in the prime factorization. For example, if k = 6, the LCM of {1, 2, 3, 4, 5, 6} is 60. The number 60 has one trailing zero because it contains one factor of 10 (2 × 5). Algorithm ...

Read More

Program to Find Out the Maximum Final Power of a List in Python

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

The power of a list is defined as the sum of (index + 1) * value_at_index over all indices. We can represent this mathematically as: $$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$ Given a list of N positive integers, we can select any single value and move it to any position (beginning, end, or leave it unchanged). Our goal is to find the maximum possible final power of the list, with the result modded by 10^9 + 7. Understanding the Problem Let's first understand what power means with a simple example: def calculate_power(nums): power ...

Read More
Showing 5821–5830 of 61,299 articles
« Prev 1 581 582 583 584 585 6130 Next »
Advertisements