Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 38 of 377

Program to find decode XORed permutation in Python

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

Suppose we have an array enc that represents an encoded permutation. There is an array perm that is a permutation of the first n (odd) positive integers. This list is encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We need to find the original array perm. So, if the input is like enc = [2, 5, 6, 3], then the output will be [7, 5, 0, 6, 5]. Here [7 XOR 5, 5 XOR 0, 0 XOR 6, 6 XOR 5] = [2, 5, 6, 3]. Algorithm To solve this, we ...

Read More

Program to find minimum number of people to teach in Python

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

Suppose we have a number n, an array called languages, and an array called friendships. There are n languages numbered from 1 through n, languages[i] represents a set of languages the ith user knows, and friendships[i] holds a pair [ui, vi] denoting a friendship between users ui and vi. We can select one language and teach it to some users so that all friends can communicate with each other. We have to find the minimum number of users required to teach. Note that friendships are not transitive, so if x is a friend of y and y is a ...

Read More

Program to find minimum cost to hire k workers in Python

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

Suppose we have an array called quality for each different worker, and have another array called wages and a value K. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. We want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules: Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group. Every worker in the paid ...

Read More

Program to check a string can be split into three palindromes or not in Python

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

Given a string, we need to check whether it can be split into exactly three palindromic substrings. A palindrome reads the same forwards and backwards. So, if the input is like s = "levelpopracecar", then the output will be True because we can split it like "level", "pop", "racecar" − all are palindromes. Algorithm To solve this, we will follow these steps − Create a 2D DP table to store which substrings are palindromes Fill the DP table using dynamic programming Try all possible ways to split the string into three parts Check if all ...

Read More

Program to find minimum cost to merge stones in Python

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

The minimum cost to merge stones problem involves merging K consecutive piles of stones into one pile, where the cost equals the total stones in those K piles. We need to find the minimum cost to merge all piles into a single pile. This is a classic dynamic programming problem that uses interval DP to find the optimal way to merge stone piles. Understanding the Problem Given N piles of stones and a merge factor K, we can only merge exactly K consecutive piles at a time. The key insight is that for a solution to exist, ...

Read More

Program to find number of squareful arrays in Python

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

Given a target string and a stamp, we need to find the sequence of stamp positions that can transform a string of question marks into the target string. This is a reverse engineering problem where we work backwards from the target to find valid stamping positions. Problem Understanding We start with a sequence of '?' marks and use a stamp to gradually build the target string. The key insight is to work backwards − start from the target string and figure out which stamp positions could have created it. Algorithm Steps The algorithm follows these steps ...

Read More

Program to find number of distinct subsequences in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 902 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 222 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 545 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 265 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
Showing 371–380 of 3,768 articles
« Prev 1 36 37 38 39 40 377 Next »
Advertisements