Found 33676 Articles for Programming

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

308 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

397 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

262 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

469 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

293 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

Program to count number of stepping numbers of n digits in python

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

514 Views

Suppose we have a number n we have to find the number of stepping numbers of n digit. As we know a number is called stepping number when all adjacent digits have an absolute difference of 1. So if a number is 123, this is stepping number but 124 is not. If the answer is very large then return result mod 10^9 + 7.So, if the input is like n = 2, then the output will be 17, as the stepping numbers are [12, 23, 34, 45, 56, 67, 78, 89, 98, 87, 76, 65, 54, 43, 32, 21, 10]To ... Read More

Program to find the size of the longest sublist where car speed is constant in python

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

151 Views

Suppose we have a list of numbers representing the position of a car at equally spaced intervals of time. We have to find the size of the longest sublist where the car was traveling at a constant speed.So, if the input is like positions = [0, 4, 8, 12, 6, 4, 0], then the output will be 4, as the sublist is [0, 4, 8, 12].To solve this, we will follow these steps −j := 1max_cnt := 0, current := 0distance := |positions[0] - positions[1]|while j < size of positions, doprev := positions[j - 1]if distance is same as |positions[j] ... Read More

Program to check given push pop sequences are proper or not in python

Arnab Chakraborty
Updated on 02-Dec-2020 04:58:16

236 Views

Suppose we have a list of numbers called pushes, and another list of numbers called pops, we have to check whether this is a valid sequence of stack push and pop actions or not.So, if the input is like pushes = [1, 2, 5, 7, 9] pops = [2, 1, 9, 7, 5], then the output will be True, as we can push [1, 2] first then pop them both. Then push [5, 7, 9] and pop them all.To solve this, we will follow these steps −s := a new stacki := 0for each ele in pushes, dopush ele into ... Read More

Program to find number of square submatrices with 1 in python

Arnab Chakraborty
Updated on 02-Dec-2020 04:56:43

135 Views

Suppose we have a 2D binary matrix, we have to find the total number of square submatrices with all 1 s are there.So, if the input is like11101110111000001011then the output will be 17 as there are 12 (1 x 1) squares, 4 (2 x 2) squares and 1 (3 x 3) square.To solve this, we will follow these steps −res := 0for i in range 0 to row count of matrix, dofor j in range 0 to column count of matrix, doif i is same as 0 or j is same as 0, thenres := res + matrix[i, j]otherwise when ... Read More

Program to find final states of rockets after collision in python

Arnab Chakraborty
Updated on 02-Dec-2020 04:55:07

187 Views

Suppose we have a list of numbers called nums and that is representing rocket's size and direction. Positive integer indicates right, and negative number represents left. And the absolute value of the number represents the rocket's size. Now when two rockets collide, the smaller one will be destroyed and the larger one will continue on its journey unchanged. When they are the same size rockets and they collide, they'll both destroy. When two rockets are moving in the same direction, they will never collide (assuming rockets speed are same). We have to find the state of the rockets after all ... Read More

Advertisements