Server Side Programming Articles - Page 1429 of 2646

Program to count number of paths whose sum is k in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:16:32

242 Views

Suppose we have a binary tree and another value k, we have to find the number of unique node to sub child paths are there which sums to k.So, if the input is likeand k = 5, then the output will be 2, as the paths are [2, 3] and [1, 4]To solve this, we will follow these steps −count := a map initially holds value 1 for key 0ans := 0, prefix := 0Define a function dfs() . This will take nodeif node is not null, thenprefix := prefix + value of nodeans := ans + (count[prefix - target], ... Read More

Program to check whether we can color a tree where no adjacent nodes have the same color or not in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:15:00

266 Views

Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color.So, if the input is likethen the output will be True as we can getTo solve this, we will follow these steps −colors := an empty mapprop := an empty mapDefine a function dfs() . This will take node, and flagif node is null, thenreturncolors[value of node] := ... Read More

Program to find the final ranking of teams in order from highest to lowest rank in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:13:14

396 Views

Suppose we have a list of strings called votes, here each entry is in lowercase letters and they are representing votes on candidates in order from highest to lowest preference. Here the rank of a candidate depends first by the number of votes received on the highest preference. Now if there are ties, we shall check the number of votes received on next highest preference, and so on. If there are still ties, then they will be ranked alphabetically. So we have to find the final ranking of the teams in order from highest to lowest ranked.So, if the input ... Read More

Program to count number of switches that will be on after flipping by n persons in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:12:05

501 Views

Suppose we have a number n, and there are n switches in a room, and all switches are off. Now n people who flip switches as follows −Person 1 flips all switches that are multiples of 1 (so all of the switches).Person 2 flips switches that are multiples of 2 (2, 4, 6, ...)Person i flips switches that are multiples of i.We have to find the number of switches that will be on at the end.So, if the input is like n = 5, then the output will be 2, as initially all are off [0, 0, 0, 0, 0], ... Read More

Program to count number of surrounded islands in the matrix in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:10:50

604 Views

Suppose we have a binary matrix. Where 1 represents land and 0 represents water. As we know an island is a group of 1s that are grouped together whose perimeter is surrounded by water. We have to find the number of completely surrounded islands.So, if the input is likethen the output will be 2, as there are three islands, but two of them are completely surrounded.To solve this, we will follow these steps −Define a function dfs() . This will take i, jif i and j are not in range of matrix, thenreturn Falseif matrix[i, j] is same as 0, ... Read More

Program to find sum of all numbers formed by path of a binary tree in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:08:32

339 Views

Suppose we have a binary tree where each node is containing a single digit from 0 to 9. Now each path from the root to the leaf represents a number with its digits in order. We have to find the sum of numbers represented by all paths in the tree.So, if the input is likethen the output will be 680 as 46 (4 → 6), 432 (4 → 3 → 2), 435 (4 → 3 → 5), and their sum is 913.To solve this, we will follow these steps −Define a function solve() . This will take root, string:= blank ... Read More

Program to find number of subsequence that are present inside word list in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:07:01

431 Views

Suppose we have a list of words and a string s, we have to find the number of strings in the words list that are subsequences of s.So, if the input is like words = ["xz", "xw", "y"] s = "xyz", then the output will be 2, as "xz" and "y" are subsequences of "xyz".To solve this, we will follow these steps −ans := 0d := an empty mapfor each word in words, doinsert word at the end of d[word[0]]for each c in s, dol := d[c]d[c] := a new listfor each word in l, doif size of word is ... Read More

Program to find minimum number of subsequence whose concatenation is same as target in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:05:09

296 Views

Suppose we have two strings source and target, we have to find the minimum number of subsequences of source we can form such that if we concatenate them, it will be same as target. If there is no such result, return -1.So, if the input is like source = "xyz" target = "xyzyzz", then the output will be 3, as we can concatenate these ["xyz" + "yz" + "z"]To solve this, we will follow these steps −s_size := size of s, t_size := size of tconcat_count := 0, target_idx := 0while target_idx < t_size, dosource_idx := 0temp_index := target_idxwhile source_idx ... Read More

Program to list of candidates who have got majority vote in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:03:58

511 Views

Suppose we have a list of numbers called nums where each number represents a vote to a candidate. We have to find the ids of the candidates that have greater than floor(n/3) votes, in non-decreasing order.So, if the input is like nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7], then the output will be [6, 7], as 6 and 7 have 40% of the votes.To solve this, we will follow these steps −ans := a new empty setsort the list numsi := 0n := size of numswhile i < size of nums, doif occurrences of ... Read More

Program to count maximum number of strings we can generate from list of words and letter counts in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:02:22

321 Views

Suppose we have a list of strings where each string contains two letters "A"s and "B"s. We have two values a and b. We have to find the maximum number of strings that can be formed. We can use at most a number of "A"s and at most b number of "B"s, without reuse.So, if the input is like strings = ["AAABB", "AABB", "AA", "BB"] a = 4 b = 2, then the output will be 2, as we can take the strings using 4 "A"s and 2 "B"s ["AABB", "AA"].To solve this, we will follow these steps −pairs := ... Read More

Advertisements