Found 7347 Articles for C++

C++ Program to check if given numbers are coprime or not

Arnab Chakraborty
Updated on 02-Mar-2022 07:47:17

2K+ Views

Suppose, we have n integers in an array nums. We have to find out if the numbers in the array are pairwise coprime, setwise coprime, or not coprime.Two numbers nums[i] and nums[j] are said to be pairwise coprime if gcd(nums[i], nums[j]) = 1. This should hold for every number pair in the array and i < j.The numbers are said to be setwise coprime if gcd(nums[i]) = 1.If they are neither, we say that they are not coprime.So, if the input is like n = 4, nums = {7, 11, 13, 17}, then the output will be the numbers are ... Read More

C++ Program to check if two stacks of letters can be emptied or not

Arnab Chakraborty
Updated on 02-Mar-2022 07:41:49

106 Views

Suppose, there are 2n number of letters and each of them has an integer number between 1 to n written on them. There are exactly two letters that have the same number written on them. These letters are arranged into m stacks and stack i has letters stack[i] on it. Our task is to empty all the stacks in the following mannerWe have to choose any two stacks and remove the top letter from both of them.The letters that we have removed must have the same number on both of them.If we can empty the m stacks in this manner, ... Read More

C++ Program to find out the maximum amount of money that can be made from selling cars

Arnab Chakraborty
Updated on 02-Mar-2022 07:17:43

209 Views

Suppose, there is a demand for red and blue cars for sale. An automobile company decides to sell p red cars and q blue cars of different prices. Currently, the company has 'a' number of red cars, 'b' number of blue cars, and 'c' numbers of colorless cars (the cars are yet to be painted) in their stock. The values of the different cars are given in arrays A, B, and C. So, the company has to sell p + q number of cars in a day and they have to make maximum profit from them. The colorless cars can ... Read More

C++ program to check if two images match after rotation and translation

Arnab Chakraborty
Updated on 02-Mar-2022 07:09:04

173 Views

Suppose, there are two n * n pixel square images first and second. The pixels can either be black or white. The images are given in a matrix representation where if a pixel is black it is represented as 'x' and if it is white, it is represented as '.'. We have to check the second image matches with the first image after 90° rotations and translations. If it does we return true, otherwise, we return false.So, if the input is like n = 4, first = {"..x.", "x.x.", "x.xx", "xx.."}, second = {"..xx", "x.xx", ".x.x", "..x."}, then the output ... Read More

C++ Program to find out the maximum amount of score that can be decreased from a graph

Arnab Chakraborty
Updated on 02-Mar-2022 06:59:39

226 Views

Suppose, there is a weighted, undirected graph that has n vertices and m edges. The score of the graph is defined as the addition of all the edges weights in the graph. The edge weights can be negative, and if they are removed the score of the graph increases. What we have to do, we have to make the score of the graph minimum by removing the edges from the graph while keeping the graph connected. We have to find out the maximum amount of score that can be decreased.The graph is given in an array 'edges', where each element ... Read More

C++ Program to find out the number of cells to block in a grid to create a path

Arnab Chakraborty
Updated on 02-Mar-2022 06:54:00

319 Views

Suppose, there is a grid of dimensions h * w. There is a robot in cell position (0, 0) and it has to go to the position (h - 1, w - 1). There are two types of cells in a grid, blocked and unblocked. The robot can pass through the unblocked cells but cannot pass through the blocked cells. The robot can go in four directions; it can go left, right, up, and down. But the robot may go in any direction from a cell to another (ignoring the previous cell it was in), so we have to make ... Read More

C++ to perform certain operations on a sequence

Arnab Chakraborty
Updated on 02-Mar-2022 06:47:04

317 Views

Suppose, we are given an empty sequence and n queries that we have to process. The queries are given in the array queries and they are in the format {query, data}. The queries can be of the three following types−query = 1: Add the supplied data to the end of the sequence.query = 2: Print the element at the beginning of the sequence. After that delete the element.query = 3: Sort the sequence in ascending order.Note that, query types 2 and 3 always have data = 0.So, if the input is like n = 9, queries = {{1, 5}, {1, ... Read More

C++ Program to find out the moves to read a point from another point in a 2D plane

Arnab Chakraborty
Updated on 02-Mar-2022 12:40:34

217 Views

Suppose, there are two points in a 2D plane a and b that have the coordinates (x1, y1) and (x2, y2) respectively. Currently, we are at point 'a' and we can move at a distance of 1 either vertically or horizontally. We move to point b from point a, then get back to point a, and we go to point b again. It is not allowed to move through the same points more than once except the points a and b. We have to find out the moves we will make in this whole trip, and output it. If we ... Read More

C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid

Arnab Chakraborty
Updated on 02-Mar-2022 06:37:29

245 Views

Suppose, we are given a grid of dimensions h * w that contains two types of cells, blocked and unblocked. Blocked cells mean that the cells aren't accessible and unblocked means that the cells are accessible. We represent the grid in a 2D array where the blocked cells are given as '#' and the unblocked cells are given as '.'. Now, we have to reach from an unblocked cell to another unblocked cell in the grid. We can perform only two moves, we can either go vertical or we can go horizontal. We can't move diagonally. We have to keep ... Read More

C++ Program to find out the minimum number of operations required to defeat an enemy

Arnab Chakraborty
Updated on 02-Mar-2022 12:39:00

294 Views

Suppose, we are playing a video game where the protagonist uses knives to defeat his enemies. The protagonist can use the knife for slashing the enemy or he can throw it towards the enemy. If the protagonist throws a knife, that cannot be retrieved again. The damage dealt by knife i is given in the array 'knives' where each element is of the form {slash, throw}. 'Slash' means the damage done to an enemy by slashing them with that knife and 'throw' means damage done to them by throwing that particular knife. Slashing can be done unlimited times, but a ... Read More

Advertisements