Programming Articles - Page 1178 of 3363

Program to find minimum swaps to arrange a binary grid using Python

Arnab Chakraborty
Updated on 29-May-2021 13:52:45

337 Views

Suppose we have a n x n binary matrix. We can perform an operation on it like, at one step we select two adjacent rows and swap them. We have to count number of minimum swaps required, so that all nodes above the major diagonal of the matrix is 0. If there is no such solution, then return -1.So, if the input is like010011100then the output will be 2 because −To solve this, we will follow these steps:n := row count of matrixm := make an array of size n and fill with nfor i in range 0 to n ... Read More

Program to find the winner of an array game using Python

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

1K+ Views

Suppose we have an array called arr, this contains unique elements and we also have another value k. Now consider a game where we take first two elements of the array. In each turn, we compare arr[0] with arr[1], and the larger value wins and remains at position 0 and the smaller value moves to the end of the array. This game will end when a value wins’ k consecutive rounds. We have to find the winner from the array.So, if the input is like arr = [1, 5, 6, 3, 4, 2], and k = 3, then the output ... Read More

Program to find number of good leaf nodes pairs using Python

Arnab Chakraborty
Updated on 29-May-2021 13:54:36

337 Views

Suppose we have a binary tree. and another value distance d. A pair of two different leaf nodes are said to be good, when the shortest path between these two nodes is smaller or same as distance d.So, if the input is likeAnd distance d = 4, then the output will be 2 because the pairs are (8, 7) and (5, 6) as their path length distance is 2, but (7, 5) or (8, 6) or other pairs are not good as their path length is 5 which is larger than d = 4To solve this, we will follow these ... Read More

Program to make a bulb switcher using binary string using Python

Arnab Chakraborty
Updated on 29-May-2021 13:55:18

1K+ Views

Suppose we have n bulbs in a room, these bulbs are numbered from 0 to n-1. We have to arrange them in a row from left to right. Initially, all the bulbs are turned off (0-state). We have to get the configuration represented by given target array 't' where t[i] is '1' if the ith bulb is on and '0' if it is off. We also have a switch to flip the state of the bulb. And flipping operation is defined as follows −Select any bulb index i.Flip each bulb from index i to index n - 1.We have to ... Read More

Program to find number of good ways to split a string using Python

Arnab Chakraborty
Updated on 29-May-2021 12:51:55

390 Views

Suppose we have a string s. Now a split is said to be a good split when we can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the equal. We have to find the number of good splits we can make in s.So, if the input is like s = "xxzxyx", then the output will be 2 as there are multiple ways of splitting but if we split like ("xxz", "xyx") or ("xxzx", "yx") then they are good.To solve this, we ... Read More

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

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

249 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

455 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

554 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

218 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

669 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

Advertisements