Found 7347 Articles for C++

C++ Program to count number of characters to be removed to get good string

Arnab Chakraborty
Updated on 04-Mar-2022 06:23:53

149 Views

Suppose we have a string S. S contains two types of characters in S, the 'x' and 'a'. We have to count what will be the longest string remaining after removal of few characters in S so that it becomes good string. A string is good if it has strictly more than the half of its length filled with character 'a'.So, if the input is like S = "xaxxxxa", then the output will be 3, because if we remove 4 'x's, the string will be "xaa" and this is a good string whose length is 3.StepsTo solve this, we will ... Read More

C++ program to find winner of ball removal game

Arnab Chakraborty
Updated on 04-Mar-2022 09:54:30

164 Views

Suppose we have four numbers n1, n2, k1 and k2. Consider there are 2 boxes, first one has n1 balls and second one has n2 balls. Amal and Bimal are playing the game. In one move they can take from 1 to k1 balls and throw them out, similarly second one will take 1 to k2 balls in his move. Amal starts the game and they plays alternatively. The one who cannot play his move will lose the game. We have to find who will be the winner.So, if the input is like n1 = 2; n2 = 2; k1 ... Read More

C++ program to count minimum how many minutes after there will be no new angry students

Arnab Chakraborty
Updated on 03-Mar-2022 11:37:58

135 Views

Suppose we have a string S of length n, with only two types of characters, 'A' or 'P'. There are n students in a row, the ith student is angry if S[i] = 'A', if it is 'P' it says S[i] is patient. An angry student at index i will hit patient student in index i+1 in every minute, and for the last student even it he is angry, he cannot hit anyone. After hitting a patient student, that student also gets angry. We have to find the minimum minutes for after that no new students get angry.So, if the ... Read More

C++ program to count number of minimum coins needed to get sum k

Arnab Chakraborty
Updated on 03-Mar-2022 11:24:02

159 Views

Suppose we have two numbers n and k. We have unlimited number of coins worth values 1 to n. We want to take some values whose sum is k. We can select multiple same valued coins to get total sum k. We have to count the minimum number of coins needed to get the sum k.So, if the input is like n = 6; k = 16, then the output will be 3, because (2 * 6) + 4.StepsTo solve this, we will follow these steps −c := (n + k - 1) / n return cExampleLet us see the ... Read More

C++ program to choose some numbers for which there will be no subset whose sum is k

Arnab Chakraborty
Updated on 03-Mar-2022 11:22:14

158 Views

Suppose we have two numbers n and k. We have to choose the maximum number of distinct elements from 1 to n, so that no subset whose sum is equal to k. If we can find then return the chosen numbers.So, if the input is like n = 5; k = 3, then the output will be [4, 5, 2]StepsTo solve this, we will follow these steps −for initialize i := (k + 1) / 2, when i

C++ program to find winner of typing game after delay timing

Arnab Chakraborty
Updated on 03-Mar-2022 11:20:12

244 Views

Suppose we have five numbers s, v1, v2, t1 and t2. Amal and Bimal are playing a typing game, they are playing their game online. In this game they will type a string whose length is s. Amal types one character in v1 milliseconds and Bimal types one character in v2 milliseconds. Amal's network delay is t1 milliseconds, and Bimal's network delay is t2 milliseconds.If the connection delay is t milliseconds, the competition passes for a participant as follows −Exactly after t milliseconds after the game start the participant receives the text to be entered.Right after that he starts to ... Read More

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

217 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

220 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

246 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

206 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

Advertisements