Found 10476 Articles for Python

Program to restore the array from adjacent pairs in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:39:28

254 Views

Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] represents 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. If there are multiple solutions, return any of them.So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]To solve this, we will follow these steps −my_map := an empty map to store list for different keysfor each pair (a, b) ... Read More

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

Arnab Chakraborty
Updated on 07-Oct-2021 11:33:05

779 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 likeand the nodes between which we have to find the distance between are 2 and 8; then the output will be 4.The edges between the two nodes 2 and 8 are: (2, ... Read More

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

Arnab Chakraborty
Updated on 07-Oct-2021 11:19:54

177 Views

Suppose we have one m x n matrix. and another value k. Here the value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i in range (0 to a) and j in range (0 to b). We have to find the kth largest value (1-indexed) of all the coordinates of matrix.So, if the input is like5216And k = 1, then the output will be 7 because the value of coordinate (0, 1) is 5 XOR 2 = 7, and this is the largest oneTo solve this, we will follow these steps −m := ... Read More

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

Arnab Chakraborty
Updated on 07-Oct-2021 11:10:15

183 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 operations required to get the result.So, if the input is like s = "sts", t = "uss", ... Read More

Program to find decode XORed permutation in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:04:03

225 Views

Suppose we have an array enc. There is an array perm that is a permutation of the first n(odd) positive integers. This list will be encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We have 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 XOR 0 XOR 6 XOR 5] = [2, 5, 6, 3]To solve this, we will follow these steps −n := size of encresult := an array of size ... Read More

Program to find minimum number of people to teach in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:01:44

194 Views

Suppose we have a number n, an array called 'languages', and an array called 'friendships', so 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] denotes a friendship between the 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. (One thing we have to keep in mind that friendships are not transitive, so if x is ... Read More

Program to find minimum cost to hire k workers in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:27:16

342 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 group must be paid at least their minimum wage expectation.We have to ... Read More

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

Arnab Chakraborty
Updated on 07-Oct-2021 09:22:02

586 Views

Suppose we have a string s. We have to check whether we can split s into three palindromic substring or not.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.To solve this, we will follow these steps −n := size of sdp := a matrix of order n x n and fill with falsefor i in range n-1 to 0, decrease by 1, dofor j in range 0 to n - 1, doif i >= j, thendp[i, j] := Trueotherwise when s[i] is ... Read More

Program to find minimum cost to merge stones in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:18:18

282 Views

Suppose we have N piles of stones arranged in a row. Here the i-th pile has stones[i] number of stones. A move consists of merging K consecutive piles into one pile, now the cost of this move is equal to the total number of stones in these K number of piles. We have to find the minimum cost to merge all piles of stones into one pile. If there is no such solution then, return -1.So, if the input is like nums = [3, 2, 4, 1], K = 2, then the output will be 20, because, initially have [3, ... Read More

Program to find number of squareful arrays in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:07:41

141 Views

Suppose we want to make a target string of lowercase letters. At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters. On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns.As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn. If the sequence is possible ... Read More

Advertisements