Find Number of Square Submatrices with 1 in Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:56:43

148 Views

Suppose we have a 2D binary matrix, we have to find the total number of square submatrices with all 1 s are there.So, if the input is like11101110111000001011then the output will be 17 as there are 12 (1 x 1) squares, 4 (2 x 2) squares and 1 (3 x 3) square.To solve this, we will follow these steps −res := 0for i in range 0 to row count of matrix, dofor j in range 0 to column count of matrix, doif i is same as 0 or j is same as 0, thenres := res + matrix[i, j]otherwise when ... Read More

Find Final States of Rockets After Collision in Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:55:07

194 Views

Suppose we have a list of numbers called nums and that is representing rocket's size and direction. Positive integer indicates right, and negative number represents left. And the absolute value of the number represents the rocket's size. Now when two rockets collide, the smaller one will be destroyed and the larger one will continue on its journey unchanged. When they are the same size rockets and they collide, they'll both destroy. When two rockets are moving in the same direction, they will never collide (assuming rockets speed are same). We have to find the state of the rockets after all ... Read More

Minimum Space Plane Required for Skydivers in K Days using Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:53:37

166 Views

Suppose we have a list of numbers called nums where each value represents a group of people looking to skydive together. And we have another value k representing how many days they can apply for skydiving. We have to find the minimum capacity of the plane we need to be able to fulfill all requests within k days. The requests should be fulfilled in the order they were given and a plane can only fly once a day.So, if the input is like nums = [16, 12, 18, 11, 13], k = 3, then the output will be 28, as ... Read More

Find Shortest Cycle Length Holding Target in Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:50:46

410 Views

Suppose we have adjacency list of a directed graph, where each list at index i is represented connected nodes from node i. We also have a target value. We have to find the length of a shortest cycle that contains the target. If there is no solution return -1.So, if the input is liketarget = 3., then the output will be 3, as the cycle is nodes 1 -> 2 -> 3 -> 1. Note there is another cycle 0 -> 1 -> 2 -> 3 -> 0, but this is not shortest.To solve this, we will follow these steps ... Read More

Find Total Cost for Completing All Shipments in Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:47:52

311 Views

Suppose we have a list of lists called ports, where ports[i] represents the list of ports that port i is connected to. We also have another list of lists called shipments where each list of the sequence [i, j] which denotes there is a shipment request from port i to port j. And the cost to ship from port i to port j is the length of the shortest path from the two ports, we have to find the total cost necessary to complete all the shipments.So, if the input is like ports = [[1, 4], [2], [3], [0, 1], ... Read More

Count Positive Integers with 0 as a Digit and Maximum D Digits in C++

Sunidhi Bansal
Updated on 01-Dec-2020 13:06:56

145 Views

We are given a number d which represents the number of digits. The goal is to find the count of positive integers with 0 as a digit and have maximum d digits. Count all 1 digit, 2 digit, 3 digit….d digit positive numbers containing at least one 0.We will first find numbers the count of numbers that have d digits with at least one 0. Let’s say d=3. To make a 3-digit number with at least one 0, possible ways are −Here d1 can have 1 to 9 : 9 ways d2 can have 0-9 : 10 ways d3 can ... Read More

Count the Number of Rhombi Possible Inside a Rectangle in C++

Sunidhi Bansal
Updated on 01-Dec-2020 13:04:39

225 Views

We are given a rectangle with dimensions as height X width. The rectangle is represented on a 2D coordinate system with the left-lower corner at point (0, 0). So the goal is to count the number of rhombi possible inside this rectangle such that all these conditions are met −The rhombus has an area more than 0.The diagonals of the rhombus are parallel to the x and y axis.The rhombus has integer coordinates for all corners.Let us understand with examplesInput − length=3 width=3Output − Count of number of rhombi possible inside a rectangle of given size are: 4Explanation − Below ... Read More

Count Possible Decodings of a Given Digit Sequence in C++

Sunidhi Bansal
Updated on 01-Dec-2020 13:02:52

296 Views

We are given a string representing a digit sequence. Each digit is decoded from 1 to 26 as English Alphabet. 1 is ‘A’, 2 is ‘B’ and so on till 26 as ‘Z’. The goal is to find the count of all possible decodings out of a given digit sequence. If the sequence is ‘123’ then possible decodings are ‘ABC’ ( 1-2-3 ), ‘LC’ (12-3), ‘AW’ (1-23). Count is 3.Let us understand with examples.Input − str[]=”1532”Output − Count of Possible Decodings of a given Digit Sequence are − 2Explanation − Possible decodings are AECB - (1-5-3-2) and OCB (15-3-2).Input − ... Read More

Count Rotations Divisible by 8 in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:57:52

348 Views

We are given a large number. The goal is to count the rotations of num that are divisible by 8.As the rotations can not be done again and again. We will use the divisible by 8 property. If the last three digits are divisible by 8then the number is divisible by 8. If the number is 1800 then it’s rotations will be 1800, 0180, 0018, 8001 out of 1800 is divisible by 8.Let us understand with examples.Input − num=15320Output − Count of rotations divisible by 4 are: 1Explanation − Rotations are −15320, 01532, 20153, 32015, 53201 Out of these, only ... Read More

Count Rotations Divisible by 4 in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:55:38

705 Views

We are given a large number. The goal is to count the rotations of num that are divisible by 4.As the rotations can not be done again and again. We will use the divisible by 4 property. If the last two digits are divisible by 4 then the number is divisible by 4. If the number is 1234 then it’s rotations will be 1234, 4123, 3412, 2341 out of which 3412 will be divisible by 4 as the last two digits 12 is divisible by 4.Let us understand with examples.Input − num=15324Output − Count of rotations divisible by 4 are: ... Read More

Advertisements