Found 10476 Articles for Python

Program to find number of sub-arrays with odd sum using Python

Arnab Chakraborty
Updated on 29-May-2021 12:53:52

217 Views

Suppose we have an array arr. We have to find the number of sub-arrays with odd sum. If the answer is too large then return result modulo 10^9+7.So, if the input is like arr = [8, 3, 7], then the output will be 3 because all sub arrays are [[8], [3], [7], [8, 3], [3, 7], [8, 3, 7]] Now their sum values are [8, 3, 7, 11, 10, 18] so there are three odd sum values [3, 7, 11].To solve this, we will follow these steps −freq := a list with two elements 1 and 0ans := 0prefix := ... Read More

Program to find number of nodes in the sub-tree with the same label using Python

Arnab Chakraborty
Updated on 29-May-2021 12:54:53

431 Views

Suppose we have a rooted general tree with n nodes, whose nodes are numbered from 0 to n-1. Each node has a label with lowercase English letter. The labels are given as input in labels array, where lables[i] contains label for ith node. The tree is represented by edge list where each edge e has [u, v] represents u is parent and v is child. We have to find an array A of size n, represents number of nodes in the subtree of the ith node with same label as iSo, if the input is likeHere n = 5 and ... Read More

Program to find path with maximum probability using Python

Arnab Chakraborty
Updated on 29-May-2021 12:56:07

526 Views

Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards), This graph is given as input using edge list, for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, we have to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0.So, if the input is likethen the output will be 0.24 because there are two paths from node 0 to 2, one ... Read More

Program to find number of substrings with only 1s using Python

Arnab Chakraborty
Updated on 29-May-2021 12:57:04

192 Views

Suppose we have a binary string s. We have to find the number of substrings with all characters 1's. The answer may be very large so return result mod 10^9 + 7.So, if the input is like s = "1011010", then the output will be 5 because 1. four times "1" 2. one time "11"To solve this, we will follow these steps −m := 10^9+7result := 0div := divide the binary string by splitting it using '0'for each x in div, doif x is empty, then go for next iterationresult := result + quotient of (size of x *(size of ... Read More

Program to find minimum difference between largest and smallest value in three moves using Python

Arnab Chakraborty
Updated on 29-May-2021 12:57:41

648 Views

Suppose we have an array called nums. We can change one element from this array to any value in one move. We have to find the minimum difference between the largest and smallest value of nums after preforming at most 3 moves.So, if the input is like nums = [3,7,2,12,16], then the output will be 1 because we can make given array to [1,1,0,1,1], so the maximum is 1 and minimum is 0, so the difference is 1.To solve this, we will follow these steps −if size of nums

Program to find range sum of sorted subarray sums using Python

Arnab Chakraborty
Updated on 29-May-2021 12:58:22

410 Views

Suppose we have an array nums with n positive elements. If we compute the sum of all non-empty contiguous subarrays of nums and then sort them in non-decreasing fashion, by creating a new array of n*(n+1)/2 numbers. We have to find the sum of the numbers from index left to index right (1-indexed), inclusive, in the new array. The answer may be very large so return result modulo 10^9 + 7.So, if the input is like nums = [1, 5, 2, 6] left = 1 right = 5, then the output will be 20 because here all subarray sums are ... Read More

Program to count submatrices with all ones using Python

Arnab Chakraborty
Updated on 29-May-2021 12:59:14

648 Views

Suppose we have m x n binary matrix, we have to find how many submatrices have all ones.So, if the input is like101011011then the output will be 13 as there are 6 (1x1) matrices, 3 (2, 1) matrices, 2 (1x2) matrices, 1 (3x1) matrix and 1 (4x4) matrix.To solve this, we will follow these steps −m := row count of matrixn := column count of matrixdp := a zero matrix of same size m x nfor i in range 0 to m - 1, dofor j in range 0 to n - 1, doif i is same as 0 and ... Read More

Program to count number of square submatrices with all ones using Python

Arnab Chakraborty
Updated on 29-May-2021 13:00:15

201 Views

Suppose we have m x n binary matrix, we have to find how many square submatrices have all ones.So, if the input is like.011111110111then the output will be 15, as there are 10 squares of side 1. 4 squares of side 2 and 1 square of side 3. Then total number of squares = 10 + 4 + 1 = 15.To solve this, we will follow these steps −if matrix has single one, thenreturn 1rows := row count of matrixcols := column count of matrix[0]result := 0for row in range 0 to rows - 1, dofor col in range 0 ... Read More

Program to find number of subsequences that satisfy the given sum condition using Python

Arnab Chakraborty
Updated on 29-May-2021 13:01:01

619 Views

Suppose we have an array called nums and another value k. We have to find the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is smaller or equal to k. The answers may be very large so return answer mod 10^9 + 7.So, if the input is like nums = [4,6,7,8] k = 11, then the output will be 4 because there are subsequences like[4], here minimum is 4, maximum is 4, so 4+4

Program to check if array pairs are divisible by k or not using Python

Arnab Chakraborty
Updated on 29-May-2021 13:57:57

318 Views

Suppose we have an array called nums, this array contains even number of elements, and have another value k. We have to split nums into exactly n/2 pairs such that the sum of each pair is divisible by k. If we can do so then return true, otherwise false.So, if the input is like nums = [9, 5, 3, 4, 7, 10, 20, 8] k = 3, then the output will be True because we can make pairs like (9, 3), (5, 7), (4, 20), (8, 10), sum of all pairs are divisible by 3.To solve this, we will follow ... Read More

Advertisements