Find Position of Box Occupying Given Ball in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:34:42

242 Views

Suppose we have two arrays A and B. The size of A is the number of rows and A[i] is the number of boxes in the ith row. And B is the array of balls where B[i] denotes a number on the ball. Given that ball i (value B[i]) will be placed in a box whose position from starting is B[i]. We have to find the row and column of the boxes corresponding to each B[i].So, if the input is like A = [3, 4, 5, 6], B = [1, 3, 5, 2], then the output will be [(1, 1), ... Read More

Find Player Who Rearranges Characters to Form Palindrome String in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:33:07

115 Views

Suppose we have a string S with lowercase letters, now two players are playing the game. The rules are as follows −The player wins the game, if, at any move, a player can shuffle the characters of the string to get a palindrome string.The player cannot win when he/she has to remove any character from the string.We have to keep in mind that both players play the game optimally and player1 starts the game. We have to find the winner of the game.So, if the input is like "pqpppq", then the output will be Player1 as player-1 in the first ... Read More

Find Original Matrix from Largest Elements in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:30:23

220 Views

Suppose we have two arrays A and B of size N and M respectively and we also have one N X M binary matrix where 1 denotes that there was a positive integer in the original matrix and 0 means that position is holding 0 into the original matrix also. We have to generate the original matrix so that A[i] denotes the largest element in the ith row and B[j] denotes the largest element in the jth column.So, if the input is like A = [4, 2, 3], B = [3, 1, 0, 0, 4, 0, 5] matrix, then the ... Read More

Find Task Ordering from Given Dependencies in C++

Arnab Chakraborty
Updated on 27-Aug-2020 06:28:28

440 Views

Suppose we have n different tasks; these tasks are labeled from 0 to n-1. Some tasks may have prerequisites tasks, so as an example if we want to choose task 2 then we have to first finish the task 1, which is represented as a pair − [2, 1] If we have total number of tasks and a list of prerequisite pairs, we have to find the ordering of tasks to finish all tasks. If there are more than one correct order, we can just return one of them. And if it is impossible to finish all given tasks, return ... Read More

Find Number of Sub-arrays in Permutation of First N Natural Numbers with Median M in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:25:09

335 Views

Suppose we have an array A containing the permutation of first N natural numbers and another number M is also given, where M ≤ N, we have to find the number of sub-arrays such that the median of the sequence is M. As we know the median of a sequence is defined as the value of the element which is in the middle of the sequence after sorting it according to ascending order. For even length sequence, the left of two middle elements is used.So, if the input is like A = [3, 5, 6, 4, 2] and M = ... Read More

Find Number of Spectators Standing in Stadium at Time t in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:22:49

101 Views

There are n number of spectators in the stadium, and they are labeled from 1 to n. Now follow these cases −At time t1, the first spectator stands.At time t2, the second spectator stands.…At time tk, the k-th spectator stands.At time tk + 1, the (k + 1)-th spectator stands and the first spectator sits.At time tk + 2, the (k + 2)-th spectator stands and the second spectator sits.…At time tn, the n-th spectator stands and the (n – k)-th spectator sits.At time tn + 1, the (n + 1 – k)-th spectator sits.…At time tn + k, the ... Read More

Find Number of 2x1 Rectangles in an NxM Rectangle in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:20:52

165 Views

Suppose we have two values n and m; we have to find the number of rectangles of size 2x1 that can be set inside a rectangle of size n x m. There are few conditions, that we have to consider −Any two small rectangles cannot overlap.Every small rectangle lies completely inside the bigger one. It is permitted to touch the edges of the bigger rectangle.So, if the input is liken = 3, m = 3, then the output will be 4To solve this, we will follow these steps −if n mod 2 is same as 0, thenreturn(n / 2) * ... Read More

Find Distinct Vertex Pairs with Distance k in a Tree using Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:17:52

254 Views

Suppose we have an integer k and also have a tree with n nodes, we have to count the number of distinct pairs of vertices which have a exact k distance.So, if the input is like k = 2then the output will be 4To solve this, we will follow these steps −N := 5005graph := adjacency list of size Nvertex_count := a 2d matrix of size 505 x 5005res := 0Define a function insert_edge() . This will take x, yinsert y at the end of graph[x]insert x at the end of graph[y]Define a function dfs() . This will take v, ... Read More

Find the Number of Distinct Islands in a 2D Matrix in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:13:37

532 Views

Suppose we have a binary matrix. We have to count the number of islands in it. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is 0, then return 0n ... Read More

Find Consecutive Zeros at the End After Multiplying N Numbers in Python

Arnab Chakraborty
Updated on 27-Aug-2020 06:10:40

244 Views

Suppose we have an array with n numbers, we have to return the number of consecutive zero’s at the end after multiplying all the n numbers.So, if the input is like [200, 20, 5, 30, 40, 14], then the output will be 6 as 200 * 20 * 5 * 30 * 40 * 14 = 336000000, there are six 0s at the end.To solve this, we will follow these steps −Define a function count_fact_two() . This will take ncount := 0while n mod 2 is 0, docount := count + 1n := n / 2 (only the quotient as ... Read More

Advertisements