Found 7347 Articles for C++

C++ code to check grasshopper can reach target or not

Arnab Chakraborty
Updated on 29-Mar-2022 09:10:54

237 Views

Suppose we have a string S of size n and another number k. The string contains four types of characters. Consider there are few cells, a grasshopper wants to jump to reach the target. Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. 'G' means that the grasshopper starts at this position and, 'T' means that the target cell. The grasshopper is able to jump exactly k cells away from its current position. We have to check whether the grasshopper can jump to the target ... Read More

C++ code to count colors to paint elements in valid way

Arnab Chakraborty
Updated on 29-Mar-2022 09:08:13

184 Views

Suppose we have an array A with n elements. We need to paint elements in colors such that −If we consider any color, all elements of this color must be divisible by the minimal element of the same color.The number of used colors should be minimized.We have to find the minimum number of colors to paint all the given numbers in a valid way.So, if the input is like A = [10, 2, 3, 5, 4, 2], then the output will be 3, because paint the first color to the elements A[0] and A[3], paint second color to the elements ... Read More

C++ code to get minimum sum of cards after discarding

Arnab Chakraborty
Updated on 29-Mar-2022 09:00:03

106 Views

Suppose we have five numbers in an array T. There are five cards and each card has a number written on them. The ith card has T[i] written on it. We can discard some cards and our goal is to minimize the sum of numbers written on remaining numbers. He is allowed to at most once discard two or three cards with the same number. We won't discard cards if it's impossible to choose two or three cards with the same number. We have to find the minimum sum possible.So, if the input is like T = [7, 3, 7, ... Read More

C++ code to find a point that satisfies the constraints

Arnab Chakraborty
Updated on 29-Mar-2022 08:54:56

281 Views

Suppose, we are given two points a = (x1, y1) and b = (x2, y2). The Manhattan distance between two points is dist(a, b) = |x1 - x2| + |y1 - y2|. If the point a's coordinate is (0, 0) and the point b's coordinate is (x, y), we have to find a point c such that dist(a, c) = dist(a, b)/ 2 and dist(b, c) = dist(a, b)/2. If a point like that is not available then, we print -1, -1.So, if the input is like x = 13, y = 7, then the output will be 6, 4.StepsTo ... Read More

C++ code for calculation of a physics experiment

Arnab Chakraborty
Updated on 29-Mar-2022 08:53:03

585 Views

Suppose, we are performing a physics experiment. We are given n pairs of values and a threshold value k. Each the first value of the pair is added to a total value and the second value of the pair is also added to another total value. Now, we check if a total value is minimum or the (k - total) value is minimum. We do this for both the totals and then add them and print the output.So, if the input is like n = 4, k = 20, values = {{3, 5}, {4, 3}, {2, 1}, {4, 4}}, then ... Read More

C++ code to find out which number can be greater

Arnab Chakraborty
Updated on 29-Mar-2022 08:50:15

387 Views

Suppose, we are given two k-digit numbers m and n. The digits of the numbers are randomly shuffled and then compared. We have to find out which number has a higher probability to be greater.So, if the input is like n = 231, m = 337, k = 3, then the output will be ‘Second’, or the second number has a higher probability to be greater.StepsTo solve this, we will follow these steps −s1 := convert n to string s2 := convert m to string f := 0, s = 0 for initialize i := 0, when i < k, ... Read More

C++ code to find out if a name is male or female

Arnab Chakraborty
Updated on 29-Mar-2022 08:47:28

780 Views

Suppose, we are given n strings in an array 'input'. The strings are names, we have to find out if they are male or female names. If a name ends with 'a', 'e', 'i', or 'y'; it can be said that it is a female name. we print 'male' or 'female' for each input in the string.So, if the input is like n = 5, input = {"Lily", "Rajib", "Thomas", "Riley", "Chloe"}, then the output will be Female, Male, Male, Female, Female.StepsTo solve this, we will follow these steps −for initialize i := 0, when i < n, update (increase ... Read More

C++ code to find the number of scans needed to find an object

Arnab Chakraborty
Updated on 29-Mar-2022 08:43:19

128 Views

Suppose, we are given a grid of dimensions m x n. An object is placed at cell (ix, iy) and we have to find the object scanning from a starting position (sx, sy). The scanning algorithm scans the ith row and the j-th column of the grid if it is located at cell (i, j) of the grid. If it has found the object, the scanning stops; if not, the scanning pointer moves to cell in position (i + 1, j + 1) and then scans in the same way. This continues until the item is found. Given the positions, ... Read More

C++ code to find the number of refill packs to be bought

Arnab Chakraborty
Updated on 29-Mar-2022 08:41:38

128 Views

Suppose, there are 'a' number of matches and 'b' number of press conferences to be held in a stadium in a particular week. There are two cafeterias, one in the player dressing room and one in the press conference area. There are two soft drink dispensers in the cafeterias and they need to be filled at the start of the week. The dressing room cafeteria drink dispenser is used heavily, and it needs to be refilled after every 'c' games and the conference area cafeteria's dispenser needs to be refilled after every 'd' events. The stadium maintenance committee can order ... Read More

C++ code to find out if everyone will get ice cream

Arnab Chakraborty
Updated on 29-Mar-2022 08:39:44

394 Views

Suppose, there are three groups of people coming to a party. The first group of people likes butterscotch ice cream and do not like any other flavors of ice cream, the second group of people only dislikes strawberry ice cream and like every other flavor, and the third group likes all kinds of ice cream. Now, there are x people of the first group, y people of the second group, and z people of the third group are coming to a party, and everyone should have at least one icecream of their liking. The party organizers have brought a packs ... Read More

Advertisements