Found 7347 Articles for C++

C++ code to check triangular number

Arnab Chakraborty
Updated on 29-Mar-2022 11:31:34

911 Views

Suppose we have a number n. We have to check whether the number is triangular number or not. As we know, if n dots (or balls) can be arranged in layers to form a equilateral triangle then n is a triangular number.So, if the input is like n = 10, then the output will be True.StepsTo solve this, we will follow these steps −for initialize i := 1, when i

C++ code to find minimal tiredness after meeting

Arnab Chakraborty
Updated on 29-Mar-2022 11:29:38

94 Views

Suppose we have two numbers a and b. Two friends are at position x = a and x = b on OX axis. Each of the friends can move by one along the line in any direction unlimited number of times. By moving, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2 and so on. Two of them want to meet one integer point on OX axis. We have to find the minimum total tiredness they should gain.So, if the input is ... Read More

C++ code to find corrected text after double vowel removal

Arnab Chakraborty
Updated on 29-Mar-2022 11:27:52

132 Views

Suppose we have a string S with n character. On a text editor, there is a strange rule. The word corrector of this text editor works in such a way that as long as there are two consecutive vowels in the word, it deletes the first vowel in a word. If there are no two consecutive vowels in the word, it is considered to be correct. We have to find corrected word from S. Here vowels are 'a', 'e', 'i' 'o', 'u' and 'y'.So, if the input is like S = "poor", then the output will be "por".StepsTo solve this, ... Read More

C++ code to count steps to reach final position by robot

Arnab Chakraborty
Updated on 29-Mar-2022 11:24:46

286 Views

Suppose we have two coordinates (x1, y1) and (x2, y2). A robot is at the point (x1, y1) and wants to go to the point (x2, y2). In a single step, the robot can move towards one cell to its 8 adjacent coordinates. We have to find minimal number of steps needed to reach the final position.So, if the input is like x1 = 3; y1 = 4; x2 = 6; y2 = 1;, then the output will be 3, becauseStepsTo solve this, we will follow these steps −return maximum of |x2 - x1| and |y2 - y1|ExampleLet us see ... Read More

C++ code to count numbers after division elements greater than half of array size

Arnab Chakraborty
Updated on 29-Mar-2022 11:20:32

107 Views

Suppose we have an array A with n elements. We have to find some non-zero integer d, such that such that, after each number in the array is divided by d, the number of positive values that are presented in the array is greater than or equal to the half of the array size. If there are multiple values of d that satisfy the condition. If there are multiple answers, return any one of them.So, if the input is like A = [10, 0, -7, 2, 6], then the output will be 4, because here n = 5 , so ... Read More

C++ code to find who cannot give sufficient candies

Arnab Chakraborty
Updated on 29-Mar-2022 11:17:25

101 Views

Suppose we have two numbers a and b. There are a and b number of candies in Amal's and Bimal's hand. Amal offered 1 candy to Bimal and Bimal gave two candies to Amal, in the next turn Amal gave 3 candies and Bimal gave 4 and so on. This continued until the moment when one of them couldn’t give the right amount of candy. They don’t consider the candies they have got from the opponent, as their own. We have to find, who is the first can’t give the right amount of candy.So, if the input is like a ... Read More

C++ code to find minimum number starting from n in a game

Arnab Chakraborty
Updated on 29-Mar-2022 11:12:58

150 Views

Suppose we have a number n. In a game initially the value of n is v and the player is able to do the following operation zero or more times: Select a positive integer x that x < n and x is not a divisor of n, then subtract x from n. The goal of the player is to minimize the value of n in the end.So, if the input is like n = 8, then the output will be 1, because the player can select x = 3 in the first turn, then n becomes 5. We can then ... Read More

C++ code to find money after buying and selling shares

Arnab Chakraborty
Updated on 29-Mar-2022 11:11:01

246 Views

Suppose we have two arrays A of size n, and B of size m, and another number r. There are n opportunities to buy shares. The i-th of them allows to buy as many shares as we want, ith share price is A[i]. And also there are m opportunities to sell shares. The i-th of them allows to sell as many shares as we want, sell price of ith share is B[i]. We can't sell more shares than we have. If we have r amount of money and no existing share, we have to find the maximum number of money ... Read More

C++ code to find array from given array with conditions

Arnab Chakraborty
Updated on 29-Mar-2022 11:07:41

77 Views

Suppose we have an array A with n elements. There is another hidden array B of size n. The elements can be negative or positive. For each index i in range 1 to n, following operations will be performed −Set A[i] as 0 initiallyThen add B[i] to A[i], subtract B[i+1], then add B[i+2] and so onWe have to find the array B.So, if the input is like A = [6, -4, 8, -2, 3], then the output will be [2, 4, 6, 1, 3]StepsTo solve this, we will follow these steps −for initialize i := 0, when i < size ... Read More

C++ code to find minimum stones after all operations

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

184 Views

Suppose we have a string S with n characters. The characters will be either '+' or '-'. There is a pile of stones, n times we either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile. We have to find the minimal possible number of stones that can be in the pile after making these operations. If we took the stone on i-th operation, S[i] is equal to "-", if added, S[i] is equal to "+".So, if the input is like S ... Read More

Advertisements