Found 26504 Articles for Server Side Programming

Program to find minimum number of vertices to reach all nodes using Python

Arnab Chakraborty
Updated on 29-May-2021 13:44:03

485 Views

Suppose we have a directed acyclic graph, with n vertices and nodes are numbered from 0 to n-1, the graph is represented by an edge list, where edges[i] = (u, v) represents a directed edge from node u to node v. We have to find the smallest set of vertices from which all nodes in the graph are reachable. (We can return the vertices in any order).So, if the input is likethen the output will be [0, 2, 3] because these two vertices are not reachable from any other vertices, so if we start from them we can cover all.To ... Read More

Program to find minimum operations to make array equal using Python

Arnab Chakraborty
Updated on 29-May-2021 13:44:41

274 Views

Suppose we have a value n, consider an array nums with n elements, where arr[i] = (2*i)+1 for all i. Now in one operation, we can choose two indices x and y where 0 =0, doans:= ans + (n-j)q := q - 1j := j + 2return ansLet us see the following implementation to get better understanding −Example Live Demodef solve(n):    ans=0    if n==1:       return ans    q=(n//2)-1    j=1    while q>=0:       ans=ans+(n-j)       q-=1       j+=2    return ans n = 4 print(solve(n))Input4Output4

Program to find maximum number of non-overlapping subarrays with sum equals target using Python

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

195 Views

Suppose we have an array nums and another value called target. Now we have to find the maximum number of non-empty non-overlapping subarrays such that the sum of values in each different subarray is same as target.So, if the input is like nums = [3, 2, 4, 5, 2, 1, 5] target = 6, then the output will be 2 as there are two subarrays [2, 4] and [1, 5] whose sum is same as 6.To solve this, we will follow these steps −t := a new set with single element 0temp := 0ans:= 0for each i in nums, dotemp ... Read More

Program to find Kth bit in n-th binary string using Python

Arnab Chakraborty
Updated on 29-May-2021 13:46:03

389 Views

Suppose we have two positive values n and k, now we can make a binary string S_n by using following rules −S_1 = 0S_i = S_i-1 concatenate "1" concatenate reverse(invert(S_i-1)) for i > 1Here reverse(x) returns the reversed string x, and invert(x) flips all the bits in x.These are the example of four such stringsS_1 = "0"S_2 = "011"S_3 = "0111001"S_4 = "011100110110001"We have to find kth bit in S_n.So, if the input is like n = 4 k = 10, then the output will be 1 because S_4 = "011100110110001", so 10th bit is 1 (first bit is at ... Read More

Program to find minimum insertions to balance a parentheses string using Python

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

802 Views

Suppose we have a string s with opening and closing parenthesis '(' and ')'. We can say a parentheses string is balanced when −Any left parenthesis '(' have a corresponding two consecutive right parenthesis '))'.A Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.So for example, "())", "())(())))" are balanced but ")()", "()))" are not. If we have such string, we have to count number of parenthesis (opening or closing) to make string balanced.So, if the input is like s = "(())))))", then the output will be 1 because if we split it up, we can ... Read More

Program to check whether we can convert string in K moves or not using Python

Arnab Chakraborty
Updated on 29-May-2021 13:48:43

207 Views

Suppose we have two strings s and t, we have to check whether s can be converted to t in k moves or less. In ith move you can do these operations.Select any index j (starting from 1) in s, such that 1

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

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

311 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

311 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

Advertisements