Find Size of Longest Sublist with Constant Car Speed in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:00:22

176 Views

Suppose we have a list of numbers representing the position of a car at equally spaced intervals of time. We have to find the size of the longest sublist where the car was traveling at a constant speed.So, if the input is like positions = [0, 4, 8, 12, 6, 4, 0], then the output will be 4, as the sublist is [0, 4, 8, 12].To solve this, we will follow these steps −j := 1max_cnt := 0, current := 0distance := |positions[0] - positions[1]|while j < size of positions, doprev := positions[j - 1]if distance is same as |positions[j] ... Read More

Check Given Push-Pop Sequences in Python

Arnab Chakraborty
Updated on 02-Dec-2020 04:58:16

263 Views

Suppose we have a list of numbers called pushes, and another list of numbers called pops, we have to check whether this is a valid sequence of stack push and pop actions or not.So, if the input is like pushes = [1, 2, 5, 7, 9] pops = [2, 1, 9, 7, 5], then the output will be True, as we can push [1, 2] first then pop them both. Then push [5, 7, 9] and pop them all.To solve this, we will follow these steps −s := a new stacki := 0for each ele in pushes, dopush ele into ... Read More

Find Number of Square Submatrices with 1 in Python

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

166 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

204 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

175 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

425 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

322 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

166 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

247 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

305 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

Advertisements