Programming Articles

Page 297 of 2547

Program to restore the array from adjacent pairs in Python

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

Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] representing that the elements ui and vi are adjacent in an array called nums. In nums there are n unique elements. We have to find the array nums. So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]. Algorithm To solve this, we will follow these steps ? my_map := an empty map to store adjacency list for different ...

Read More

Program to find out distance between two nodes in a binary tree in Python

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

Suppose, we are given a binary tree and are asked to find the distance between two nodes in the binary tree. We find out the edges between the two nodes like in a graph and return the number of edges or the distance between them. A node of a tree has the structure as below − data : right : left : So, if the input is like 5 ...

Read More

Program to find k-th largest XOR coordinate value in Python

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

Suppose we have one m x n matrix and another value k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i is in range (0 to a) and j is in range (0 to b). We have to find the k-th largest value (1-indexed) of all the coordinates of the matrix. Problem Example Consider the following matrix: 5 2 1 6 If k = 1, then the output will be 7 because the value of coordinate (0, 1) is 5 XOR ...

Read More

Program to change minimum characters to satisfy one of three conditions in Python

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

Suppose we have two strings s and t with only lowercase letters. In one operation, we can change any character in s or t to any lowercase letter. We have to satisfy one of the following three conditions ? Every letter in s is strictly smaller than every letter in t in the alphabet. Every letter in t is strictly smaller than every letter in s in the alphabet. Both s and t consist of only one distinct letter. We have to find the minimum number of ...

Read More

Program to find decode XORed permutation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 279 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 253 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 426 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 651 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 343 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 196 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
Showing 2961–2970 of 25,466 articles
« Prev 1 295 296 297 298 299 2547 Next »
Advertisements