Programming Articles

Page 298 of 2547

Program to find number of distinct subsequences in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 912 Views

Given a string s, we need to count the number of distinct subsequences in the string. A subsequence is formed by deleting some (possibly zero) characters from the original string while maintaining the relative order of remaining characters. If the answer is large, return the result modulo 10^9 + 7. For example, if the input is s = "bab", the output will be 6 because there are 6 different subsequences: "" (empty), "b", "a", "ba", "ab", and "bab". Algorithm We use dynamic programming to solve this problem efficiently ? Create a dp array of size ...

Read More

Program to check existence of edge length limited paths in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 233 Views

Given an undirected weighted graph with n nodes and an edgeList where each edge is represented as (u, v, w) indicating a path from node u to node v with distance w. We also have queries in the format (p, q, lim) asking whether there exists a path from node p to node q with total distance less than lim. This problem can be efficiently solved using the Union-Find (Disjoint Set Union) data structure combined with sorting techniques. Algorithm Overview The approach involves: Sort edges by weight in ascending order Sort queries by their limit ...

Read More

Program to minimize deviation in array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 552 Views

Suppose we have an array nums. We can perform two types of operations on any element of the array any number of times ? For even elements, divide it by 2 For odd elements, multiply it by 2. The deviation of the array is the maximum difference between any two elements in the array. We have to find the minimum deviation the array can have after performing some number of operations. So, if the input is like nums = [6, 3, 7, 22, 5], then the output will be 5 because we can make our ...

Read More

Program to distribute repeating integers in Python

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

When distributing items to customers, we need to ensure each customer gets exactly the quantity they ordered, with all items being identical. This problem involves checking if we can satisfy all customer demands using available items. The approach uses backtracking with frequency counting. We count how many times each frequency appears, then try to allocate items to customers in descending order of demand. Algorithm Steps Here's how the solution works ? Count frequency of each unique number in the array Count frequency of frequencies (how many numbers ...

Read More

Program to find minimum one bit operations to make integers zero in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 407 Views

We need to transform a number n into 0 using specific bit operations. The operations allowed are: Select the rightmost bit in the binary representation of n. Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. We need to find the minimum number of operations required to transform n into 0. Understanding the Problem For example, if n = 6, the binary representation is "110". The transformation steps are ? Start: "110" (6 ...

Read More

Program to find out how many transfer requests can be satisfied in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 165 Views

Suppose there are n hostel rooms numbered from 0 to n-1. Students want to transfer to different rooms and place transfer requests. Since no room can remain vacant, a transfer is only satisfied if another student takes the transferring student's place. We need to find the maximum number of requests that can be satisfied simultaneously. Problem Understanding For a set of transfer requests to be valid, the number of students leaving each room must equal the number of students entering that room. This creates a balanced system where no room becomes empty or overcrowded. Example If ...

Read More

Program to find out the shortest path to reach the goal in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 475 Views

Finding the shortest path in a grid is a classic problem that can be solved using Breadth-First Search (BFS). In this problem, we need to navigate through a grid where different symbols represent different cell types and find the minimum moves to reach the goal. Grid Symbol Meanings '#' is the goal cell that we want to reach 'O' is a free cell via which we can travel to the goal cell '*' is our current position in the grid 'X' is a blocked cell, via which we cannot travel Problem Example Given the ...

Read More

Program to find largest submatrix with rearrangements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 377 Views

Given an m x n binary matrix, we can rearrange columns to find the largest submatrix containing only 1s. This problem combines dynamic programming with greedy optimization to maximize the area. Problem Understanding Consider this input matrix ? 0 0 1 1 1 1 1 0 1 After rearranging columns, we can get ? 1 1 0 1 1 1 0 1 1 The highlighted 2x2 submatrix has area 4, which is the maximum possible. Algorithm Steps ...

Read More

Program to find tuple with same product in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 569 Views

Given an array nums with unique positive values, we need to find the number of tuples (a, b, c, d) such that a*b = c*d where a, b, c, and d are elements of nums, and all elements are distinct. For example, if the input is nums = [2, 3, 4, 6], then the output will be 8 because we can get tuples like (2, 6, 3, 4), (2, 6, 4, 3), (6, 2, 3, 4), (6, 2, 4, 3), (3, 4, 2, 6), (4, 3, 2, 6), (3, 4, 6, 2), (4, 3, 6, 2). Algorithm ...

Read More

Python program to find list of triplets for which i+j+k is not same as n

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

Sometimes we need to find all possible combinations of three numbers that don't sum to a specific value. This problem involves generating all triplets (i, j, k) where each value is within given ranges and their sum is not equal to a target number n. We can solve this efficiently using list comprehension with a filtering condition to exclude triplets that sum to n. Problem Statement Given three upper bounds i, j, and k, and a target sum n, find all triplets [x, y, z] where: x ranges from 0 to i (inclusive) y ranges ...

Read More
Showing 2971–2980 of 25,466 articles
« Prev 1 296 297 298 299 300 2547 Next »
Advertisements