Found 26504 Articles for Server Side Programming

Program to find maximum value of k for which we can maintain safe distance in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:13:10

164 Views

Suppose we have a binary matrix. Here 0 signifies an empty cell, and a 1 signifies a cell with a person. The distance between two cells is the maximum value between the difference in x coordinates and the difference in y coordinates. Now matrix is considered safe with a factor k if there is an empty square such that the distance from the cell to each person in the matrix, and each side of the matrix is all greater or equal to k. We have to find the maximum value of factor k for which we can be safe.So, if ... Read More

Program to find size of smallest sublist whose sum at least target in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:10:54

206 Views

Suppose we have a list of numbers called nums, and an another input called target, we have to find the size of the shortest sublist such that its sum value is same as target or larger. If there is no such sublist then return -1.So, if the input is like nums = [2, 11, -4, 17, 4] target = 19, then the output will be 2, as we can select [17, 4] to get sum of at least 19.To solve this, we will follow these steps −ps := a list with only one element 0for each num in nums, doinsert ... Read More

Program to find smallest difference between picked elements from different lists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:08:58

173 Views

Suppose we have a list of lists, we have to find the smallest difference that can be formed by picking one value from each of the lists and taking the difference between the maximum and the minimum number of the picked element.So, if the input is like lists = [ [30, 50, 90], [85], [35, 70]], then the output will be 20, as we can take 90, 85, 70 and 90 - 70 = 20To solve this, we will follow these steps −maxVal := -infret := infdefine a priority queue pqn := size of listsfor initialize i := 0, when ... Read More

Program to find a triplet nums[i] < nums[k] < nums[j] from a list nums in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:57:29

299 Views

Suppose we have a list of numbers called nums, we have to check whether there are triplets (i, j, k) such that i < j < k and nums[i] < nums[k] < nums[j].So, if the input is like nums = [2, 12, 1, 4, 4], then the output will be True, as [2, 12, 4] matches the criteria because 2 < 4 < 12.To solve this, we will follow these steps −n := size of numsDefine an array left of size nleft[0] := nums[0]for initialize i := 1, when i < n, update (increase i by 1), do −left[i] := ... Read More

Program to remove all islands which are completely surrounded by water in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:55:56

735 Views

Suppose we have a binary matrix where 1 represents land and 0 represents water. And an island is a group of 1's that are surrounded by 0s (water) or by the edges. We have to find all of the islands that are completely surrounded by water and modify them into 0s. As we know an island is completed surrounded by water if all of the neighbors (horizontal and vertical not diagonal) are 0s (none of the neighbors are edges).So, if the input is like10000110011001100001then the output will be10000000000000000001To solve this, we will follow these steps −row := row count of ... Read More

Program to find length of shortest supersequence in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:52:38

176 Views

Suppose we have two strings s and t. We have to find the length of the shortest string that has both s and t as subsequences.So, if the input is like s = "pipe" t = "people", then the output will be 7, as one possible supersequence is "pieople".To solve this, we will follow these steps −m := size of s, n := size of ttable := a table of size (n + 1) x (m + 1) and fill with 0for i in range 0 to m, dofor j in range 0 to n, doif i is same as ... Read More

Program to find distance of shortest bridge between islands in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:50:57

454 Views

Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands.So, if the input is like001101100then the output will be 1. This will connect (1, 0) to (1, 2) points.To solve this, we will follow these steps −row := row count of matrixcol := column count of matrixDefine a function dfs() . This will take i, j, sif (i, j) is ... Read More

Query for ancestor-descendant relationship in a tree in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:48:39

269 Views

In this problem, we are given an N vertex tree and Q queries each consisting of two values i and j. Our task is to create a program to solve a Query for ancestor-descendant relationship in a tree.To solve each query, we need to check whether the node i is the ancestor of node j in the tree.Let’s take an example to understand the problem, InputQ = 2, query[][] = {{3, 5}, {1, 6}}OutputNo YesExplanationi = 3, j = 5: The node 3 is not the ancestor of node 5. Return NO. i = 1, j = 6: The node ... Read More

Program to evaluate s-expression as string in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:47:31

534 Views

Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the s-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /.So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3.To solve this, we will follow these ... Read More

Queries to update a given index and find gcd in range in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:46:39

258 Views

In this problem, we are given an array arr[] of size N and Q queries which can be of two types. Our task is to create a program to solve the queries to update a given index and find GCD in the range.Queries are −Type 1 − {1, index, value} - increase the element at given index by value.Type 2 − {2, L, R} - find the GCD of elements in the index range [L, R].Problem Description − We need to find the GCD of the elements that are in the range [L, R] and return the value.Let’s take an ... Read More

Advertisements