Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 333 of 855
Program to find out distance between two nodes in a binary tree in Python
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 MoreProgram to find k-th largest XOR coordinate value in Python
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 MoreProgram to change minimum characters to satisfy one of three conditions in Python
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 MoreProgram to find decode XORed permutation in Python
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 MoreProgram to find minimum number of people to teach in Python
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 MoreProgram to find minimum cost to hire k workers in Python
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 MoreProgram to check a string can be split into three palindromes or not in Python
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 MoreProgram to find minimum cost to merge stones in Python
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 MoreProgram to find number of squareful arrays in Python
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 MoreProgram to find number of distinct subsequences in Python
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