Found 7197 Articles for C++

C++ code to count number of notebooks to make n origamis

Arnab Chakraborty
Updated on 15-Mar-2022 05:45:32

212 Views

Suppose we have two numbers n and k. In a party there are n invited friends. Amal wants to make invitations in the form of origami. For each invitation, he needs two red papers, five green papers, and eight blue papers. There are infinite number of notebooks of each color, but each notebook consists of only one color with k papers. We have to find the minimum number of notebooks that Amal needs to buy to invite all n of his friends.So, if the input is like n = 3; k = 5, then the output will be 10, because ... Read More

C++ code to find two substrings with one minimal substring

Arnab Chakraborty
Updated on 15-Mar-2022 05:43:29

208 Views

Suppose we have a lowercase string S with n characters. We have to find two non-empty substrings P and Q, such that −Both P and Q are subsequences of SFor each index i, S[i] belong to exactly one of P and Q.P is lexicographically minimum as possible.So, if the input is like S = "thelightsaber", then the output will be 10, because we need 2 red notebooks, 3 green notebooks, and 5 blue notebooks.StepsTo solve this, we will follow these steps −c := S sort the array c a := position of (c[0]) in S delete c from S print ... Read More

C++ program to find winner of card game

Arnab Chakraborty
Updated on 15-Mar-2022 05:41:48

688 Views

Suppose we have a number n, two arrays A and B of size k1 and k2 respectively. Amal and Bimal are playing interesting card game. There are n cards, numbered 1 to n. Initially the cards are distributed between them. The game goes as follows: on each turn, each player takes one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. Then, both cards are revealed, and the player, whose card number is larger, takes both cards in his hand. Every card may be played any amount ... Read More

C++ code to find final number after min max removal game

Arnab Chakraborty
Updated on 15-Mar-2022 05:36:51

259 Views

Suppose we have an array A with n elements. There are n numbers written on a board. Amal and Bimal are playing a turn based game. In each turn, they select a number and remove it from board. Amal plays first. Amal wants to minimize the last number that he would left on the board, and Bimal wants to maximize it. We have to find the number which will remain on the board.So, if the input is like A = [2, 1, 3], then the output will be 2, because Amal will remove 3, Bimal will remove 1, so the ... Read More

C++ code to find how many upgrade to make HP greater than current

Arnab Chakraborty
Updated on 15-Mar-2022 05:34:58

296 Views

Suppose we have a number n. In a game, every character has four different health points (HP). The categories are as follows −Category A : If HP is in the form (4n + 1)Category B : If HP is in the form (4n + 3)Category C : If HP is in the form (4n + 2)Category D : If HP is in the form 4nThese 4 categories ordered from highest to lowest as A > B > C > D. So, category A is the highest and category D is the lowest. While playing the game, players can increase the ... Read More

C++ code to check we flew to Florida more than Seattle

Arnab Chakraborty
Updated on 15-Mar-2022 05:32:57

88 Views

Suppose we have a string S with two kinds of letters 'S' and 'F'. If S[i] is 'S' we are at Seattle on ith day, and if it is 'F' we are at Florida. We have to check whether we flew more times from Seattle to Florida than Florida to Seattle.So, if the input is like S = "SSFFSFFSFF", then the output will be True.StepsTo solve this, we will follow these steps −n := size of S if S[0] is same as 'S' and S[n - 1] is same as 'F', then:    return true Otherwise    return falseExampleLet us ... Read More

C++ code to find pair of numbers where one is multiple of other

Arnab Chakraborty
Updated on 15-Mar-2022 05:25:28

354 Views

Suppose we have two numbers l and r. We have to find such pair (x, y), such that l

C++ code to count number of dice rolls to get target x

Arnab Chakraborty
Updated on 15-Mar-2022 05:24:02

275 Views

Suppose we have a number x. We have a six-faced dice and its faces are numbered from 2 to 7. We want exactly x points from the dice. When we throw the dice the face number will be added up to reach our target. We do not really care about the number of dice rolls, so we just want to know any number of rolls we can make to be able to get exactly x points for them. We are very lucky, so if the probability to get x points with chosen number of rolls is non-zero, we will be ... Read More

C++ code to find three numbers whose sum is n

Arnab Chakraborty
Updated on 15-Mar-2022 06:05:59

410 Views

Suppose we have a number n. We are going to find three numbers a, b and c, such that a + b + c = n and none of these three numbers are multiple of 3.So, if the input is like n = 233, then the output will be [77, 77, 79]StepsTo solve this, we will follow these steps −if (n - 2) mod 3 is same as 0, then:    return 1, 2, and n - 3 Otherwise    return 1, 1, and n - 2ExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int n){    if ((n - 2) % 3 == 0)       cout

C++ code to check string is diverse or not

Arnab Chakraborty
Updated on 15-Mar-2022 05:12:34

316 Views

Suppose we have a string S with n lowercase letters. A string is called diverse if it has consecutive letters of the English alphabet and each letter occurs exactly once. (letters 'a' and 'z' are not adjacent). We have to check whether it is diverse or not.So, if the input is like S = "fced", then the output will be True.StepsTo solve this, we will follow these steps −sort the array S flag := 1 for initialize i := 1, when i < size of S and flag is non-zero, update (increase i by 1), do:    if S[i] - ... Read More

Advertisements