Programming Articles - Page 972 of 3363

Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:08:35

301 Views

Suppose we are given a matrix that contains integer values. We have to find out the submatrices from the matrix where the sum of elements of the matrices is equal to a given target sum value. We return the number of submatrices.So, if the input is like0010010001011101and target = 5, then the output will be 3.The number of submatrices whose sum of elements is equal to 6 is 2.To solve this, we will follow these steps −n := size of matm := (if n is same as 0, then 0, otherwise size of mat[0])if m > n, then −Define one ... Read More

Program to find out the number of non-zero submatrices in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:02:14

494 Views

Suppose we are given a matrix that contains only two values; 1s and 0s. We have to find out the number of submatrices in the given matrix that contains all 1s. We print the value as output.So, if the input is like0010010001011101then the output will be 12.To solve this, we will follow these steps −n := size of matrixm := size of matrix[0]Define an array add of size: n+1 x m+1.for initialize i := 0, when i < n, update (increase i by 1), do −for initialize j := 0, when j < m, update (increase j by 1), do ... Read More

Program to find minimum cost to connect each Cartesian coordinates in C++

Arnab Chakraborty
Updated on 16-Oct-2021 11:53:51

295 Views

Suppose we have a list of 2D Cartesian coordinate points (x, y). We can connect (x0, y0) and (x1, y1), whose cost is |x0 - x1| + |y0 - y1|. If we are allowed to connect any number of points, we have to find the minimum cost necessary such that every point is connected by a path.So, if the input is like points = [[0, 0], [0, 2], [0, -2], [2, 0], [-2, 0], [2, 3], [2, -3]], then the output will be 14 because, from (0, 0) to (0, 2), (0, -2), (2, 0), (-2, 0), costs are 2, ... Read More

Program to sort all elements in a given list and merge them into a string in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:24:52

275 Views

Suppose we are given a list of positive integers. We have to sort the list in descending order and then have to join all the elements in it to form a string. We return the joined string.So, if the input is like input = [415, 78, 954, 123, 5], then the output will be 954785415123To solve this, we will follow these steps −Define a function cmp() . This will take l, rif integer value of (string representation of (l) + string representation of (r)) > integer value of (string representation of (r) + string representation of (l)), thenreturn 1otherwise, return ... Read More

Program to find a path a continuous path in a rectangular area without engaging a bomb in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:20:16

374 Views

Suppose we are given an array mat where the elements are of this form [p, q, r] where p, q are geometric coordinates and r is a radius value. The items in the array are the locations of bombs in a rectangular area of a given width w. The rectangle is infinitely long and is bounded by x coordinates x = 0 to x = w. The r value in the bombs position signifies the safety radius of a bomb, meaning anything less than that radius of the bomb will engage it. So, what we have to do is to ... Read More

Program to find out is a point is reachable from the current position through given points in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:15:48

404 Views

Suppose in a 2D space a pointer is located at a point p that has coordinates (px, py). Now the pointer has to move to another point q having coordinates (qx, qy). The pointer just cannot move freely, it can travel to q if there are some points located in between. We are given an array of points "paths" that contain various coordinate points. The pointer can move to a point if it is located at (x+1, y) or (x, y+1) or (x-1, y) or (x, y-1) from the current position of the pointer. The given points in the array ... Read More

Program to find out the maximum points collectable in a game in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:11:56

290 Views

Suppose we are playing a game of cards. We are given several cards arranged linearly with a number on each of them. The numbers on the cards are randomly distributed; and at the beginning and the end of the cards, two cards are inserted with the number 1 on them. Now, in the game, we have to collect the maximum points by picking up the given cards. The cards are represented in an array 'cards' where the elements in the array represent the number of cards[i]. When we pick up card i, we collect points cards[i - 1] * cards[i] ... Read More

Program to find out if we win in a game in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:09:01

658 Views

Suppose we are playing a two-player game where there are n number of marbles and in each round, a player has to take a positive square number of marbles. If a player can't take that square number of marbles, he/she loses. So, given a number n, we have to find out if we can win the game or not. We always make the first turn and select an optimal number of marbles.So, if the input is like 14, then the output will be True. Because at the first turn, we take 9 marbles. That leaves 5 marbles from which the ... Read More

Program to find out the minimum number of intercountry travels in a road trip in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:06:24

348 Views

Suppose we have to plan a road trip that involves visiting various cities from different countries. We have a list of roads 'R', where each element is described as (x, y, cost). x signifies the starting city of the road, y signifies the destination city of the road, and cost signifies the cost of traveling by that road. We also have a list 'C' where each element is a country and an element contains the cities of that country. Now, we also have a starting city 's' and a destination city 'e', and we want to travel to the destination ... Read More

Program to find out the conversion rate of two currencies in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:01:39

286 Views

Suppose we are given three arrays; curr_a, curr_b, and conv_rate. The first array contains some currency names and so does the second one, and the array conv_rate contains the rates of conversion within an item curr_a[i] to cuur_b[i]. The item of conv_rate[i] is the conversion rate between curr_a[i] and curr_b[i]. Now, we are given two currencies src and dest. We have to find out the rate of conversion from src to dest. We return the value as output, and if it is not possible we return 0.So, if the input is like src = "INR", dest = "JPY", curr_a = ... Read More

Advertisements