Found 7197 Articles for C++

C++ program to count number of minutes needed to increase stick length to form a triangle

Arnab Chakraborty
Updated on 03-Mar-2022 11:07:55

263 Views

Suppose we have three numbers a, b and c. There are three sticks whose lengths are a, b and c. In one minute, we can pick one arbitrary stick and increase its length by 1 cm, but we cannot break sticks. We have to count the minimum number of minutes needed to increase their lengths and we can form a triangle with them.So, if the input is like a = 2; b = 3; c = 5, then the output will be 1, because by increasing any one of a or b by 1, we can make a triangle (a ... Read More

C++ program to check we can make two strings equal by swapping from third string

Arnab Chakraborty
Updated on 03-Mar-2022 11:03:25

300 Views

Suppose we have three strings S, T and U of same length n. For every index i in range 0 to n-1, we must swap U[i] with either S[i] or T[i]. So in total we have performed n swapping operations. We have to check whether after such n operations we can make string S exactly same as T.So, if the input is like S = "abc"; T = "bca"; U = "bca", then the output will be True, because for all i if we swap U[i] with S[i], it will be "bca", and T is already "bca".StepsTo solve this, we ... Read More

C++ program to find maximum possible median of elements whose sum is s

Arnab Chakraborty
Updated on 03-Mar-2022 11:00:07

349 Views

Suppose we have two numbers n and s. We have to find the maximum possible median of an array of n non-negative elements, such that the sum of elements is same as s.So, if the input is like n = 3; s = 5, then the output will be 2, because for the array [1, 2, 2], the sum is 5 and median is 2.StepsTo solve this, we will follow these steps −m := floor of (n / 2) + 1 return floor of (s / m)ExampleLet us see the following implementation to get better understanding −#include using namespace ... Read More

C++ program to find permutation with n peaks

Arnab Chakraborty
Updated on 03-Mar-2022 10:56:54

275 Views

Suppose we have two numbers n and k. We have to construct a permutation A using the numbers from 1 to n which has exactly k peaks. An index i is said to be peak of an array A, if A[i] > A[i-1] and A[i] > A[i+1]. If this is not possible, return -1.So, if the input is like n = 5; k = 2, then the output will be [2, 4, 1, 5, 3], other answers are also possible.StepsTo solve this, we will follow these steps −if k > (n - 1) / 2, then:    return -1 Define an array a of size: 101. for initialize i := 1, when i

C++ program to check three items circularly likes next item or not

Arnab Chakraborty
Updated on 03-Mar-2022 10:51:51

108 Views

Suppose we have an array A with n elements. There are n planes on Earth and they are numbered from 1 to n. The plane with number i likes plane A[i]. A[i] != i. We have to check whether there are three planes p, q, and r where p likes q, q likes r and r likes p.So, if the input is like A = [2, 4, 5, 1, 3], then the output will be True, because the triplet is [2, 4, 1].StepsTo solve this, we will follow these steps −n := size of A for initialize i := 0, ... Read More

C++ program to find perfect array of size n whose subarray is a good array

Arnab Chakraborty
Updated on 03-Mar-2022 10:48:51

196 Views

Suppose we have a number n. An array B is good if the sum of its elements is divisible by the length of this array. We can say an array A with n elements is perfect, if non-empty subarray of this array A is good and elements in A is in range 1 to 100. From the number n, we have to find an array A which is perfect.So, if the input is like n = 4, then the output will be [7, 37, 79, 49], other answers are also possible.StepsTo solve this, we will follow these steps −for initialize ... Read More

C++ program to count number of problems can be solved from left or right end of list

Arnab Chakraborty
Updated on 03-Mar-2022 10:43:31

279 Views

Suppose we have an array A with n elements and another number k is there. Consider there are n problems on a contest. Amal's problem solving skill is k. Amal always solves problem from any of the ends of a list. And He cannot solve problem whose difficulty is greater than k. He stops when the left and right problems' difficulty is greater than k. We have to count how many problems he can solve. A[i] represents the difficulty of ith problem.So, if the input is like A = [4, 2, 3, 1, 5, 1, 6, 4]; k = 4, ... Read More

C++ program to find minimum how many coins needed to buy binary string

Arnab Chakraborty
Updated on 03-Mar-2022 10:39:43

196 Views

Suppose we have three numbers c0, c1 and h, and a binary string S. We can flip any bit in S. We should pay h coins for each change. After some changes (possibly zero) we want to buy the string. To buy the string we should buy all its characters. To buy the bit 0 we should pay c0 coins, to buy the bit 1 you should pay c1 coins. We have to find the minimum number of coins needed to buy the string.So, if the input is like c0 = 10; c1 = 100; h = 1; S = ... Read More

C++ program to count how many ways two players win or make draw in dice throwing game

Arnab Chakraborty
Updated on 03-Mar-2022 10:34:33

252 Views

Suppose we have two numbers a and b. Amal and Bimal are playing a game. First each of them writes an integer from 1 to 6, then a dice is thrown. The player whose written number got closer to the number written on the paper, he wins that round, if both of them has the same difference, then that is a draw. If Amal writes the number a, and Bimal writes b, then we have to count the number of possible ways the Amal will win, number of possible draw and number of ways Bimal can win.So, if the input ... Read More

C++ program to count minimum number of binary digit numbers needed to represent n

Arnab Chakraborty
Updated on 03-Mar-2022 10:30:18

341 Views

Suppose we have a number n. A number is a binary decimal if it's a positive integer and all digits in its decimal notation are either 0 or 1. For example, 1001 (one thousand and one) is a binary decimal, while 1021 are not. From the number n, we have to represent n as a sum of some (not necessarily distinct) binary decimals. Then compute the smallest number of binary decimals required for that.So, if the input is like n = 121, then the output will be 2, because this can be represented as 110 + 11 or 111 + ... Read More

Advertisements