Programming Articles

Page 371 of 2547

Program to check whether we can convert string in K moves or not using Python

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

In this problem, we need to determine if we can convert string s to string t using at most k moves. In the i-th move, we can select an unused index and shift the character at that position i times forward in the alphabet (with wrapping from 'z' to 'a'). Problem Understanding The key insight is that in move i, we can shift a character by exactly i positions. Since the alphabet wraps around, we need to calculate how many times each required shift distance can be achieved within k moves. Algorithm Steps Check if ...

Read More

Program to find minimum swaps to arrange a binary grid using Python

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

When working with binary matrices, we sometimes need to arrange rows so that all elements above the major diagonal are 0. This can be achieved by swapping adjacent rows. Let's explore how to find the minimum number of swaps required. Problem Understanding Given an n x n binary matrix, we need to swap adjacent rows to ensure all elements above the major diagonal are 0. If no solution exists, we return -1. For example, with this matrix: 010 011 100 We need to rearrange it so that elements above the diagonal are all ...

Read More

Program to find number of good leaf nodes pairs using Python

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

A binary tree contains good leaf node pairs when the shortest path between two different leaf nodes is less than or equal to a given distance d. Consider this binary tree with distance d = 4 ? 1 2 3 4 5 6 8 ...

Read More

Program to make a bulb switcher using binary string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Suppose we have n bulbs in a room, numbered from 0 to n-1 and arranged in a row from left to right. Initially, all bulbs are turned off (0-state). We need to achieve a target configuration represented by a binary string where '1' means the bulb is on and '0' means it's off. The bulb switcher has a special flipping operation: Select any bulb index i Flip each bulb from index i to index n-1 (flip all bulbs from position i to the end) We need to find the minimum number of flips required to ...

Read More

Program to find number of good ways to split a string using Python

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

Suppose we have a string s. A split is said to be a good split when we can split s into 2 non-empty strings p and q where their concatenation equals s and the number of distinct letters in p and q are equal. We need to find the number of good splits we can make in s. For example, if the input is s = "xxzxyx", then the output will be 2. We can split it as ("xxz", "xyx") or ("xxzx", "yx") — both are good splits since each part has the same number of distinct characters. ...

Read More

Program to find number of nodes in the sub-tree with the same label using Python

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

In this problem, we have a rooted tree with n nodes numbered from 0 to n-1, where each node has a label (lowercase English letter). We need to find the count of nodes in each node's subtree that have the same label as that node. Problem Understanding Given a tree structure and node labels, we need to return an array where each element represents the count of nodes with the same label in that node's subtree (including the node itself). 0(c) ...

Read More

Program to find path with maximum probability using Python

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

Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards). This graph is given as input using an edge list, and for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, and we need to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0. Problem Example Consider the following graph: ...

Read More

Program to find minimum difference between largest and smallest value in three moves using Python

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

Given an array nums, we can change any element to any value in one move. The goal is to find the minimum difference between the largest and smallest values after performing at most 3 moves. The strategy is to either remove the largest elements or smallest elements (or a combination) to minimize the range. Since we have 3 moves, we can consider removing 0-3 smallest and 3-0 largest elements respectively. Algorithm The approach works as follows ? If array size ≤ 4, we can make all elements equal in 3 moves, so return 0 Sort ...

Read More

Program to find range sum of sorted subarray sums using Python

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

Suppose we have an array nums with n positive elements. We need to compute the sum of all non-empty contiguous subarrays of nums, sort them in non-decreasing order, and then find the sum of elements from index left to index right (1-indexed) in the sorted array. The answer should be returned modulo 10^9 + 7. Problem Understanding For example, if nums = [1, 5, 2, 6], left = 1, and right = 5: All subarray sums are: 1, 5, 2, 6, 6, 7, 8, 8, 13, 14 After ...

Read More

Program to count submatrices with all ones using Python

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

Given an m x n binary matrix, we need to count how many submatrices contain all ones. A submatrix is a contiguous rectangular area within the matrix. So, if the input matrix is ? 1 0 1 0 1 1 0 1 1 Then the output will be 13 as there are 6 (1x1) matrices, 3 (2x1) matrices, 2 (1x2) matrices, 1 (3x1) matrix and 1 (2x2) matrix. Algorithm Approach We use dynamic programming to solve this efficiently ? Create a DP matrix where dp[i][j] ...

Read More
Showing 3701–3710 of 25,466 articles
« Prev 1 369 370 371 372 373 2547 Next »
Advertisements