Found 7347 Articles for C++

C++ program to count number of operations needed to reach n by paying coins

Arnab Chakraborty
Updated on 03-Mar-2022 07:47:24

130 Views

Suppose we have five numbers, N, A, B, C, D. We start with a number 0 and end at N. We can change a number by certain number of coins with the following operations −Multiply the number by 2, paying A coinsMultiply the number by 3, paying B coinsMultiply the number by 5, paying C coinsIncrease or decrease the number by 1, paying D coins.We can perform these operations any number of times in any order. We have to find the minimum number of coins we need to reach NSo, if the input is like N = 11; A = ... Read More

C++ program to find maximum possible amount of allowance after playing the game

Arnab Chakraborty
Updated on 03-Mar-2022 07:40:24

183 Views

Suppose we have three numbers A, B and C. Consider a game: There are three "integer panels", each with a digit form 1 to 9 (both inclusive) printed on it, and one "operator panel" with a '+' sign printed on it. The player should make a formula of the form X+Y, by arranging the four panels from left to right. Then, the amount of the allowance will be equal to the resulting value of the formula.We have to find the maximum possible amount of the allowance.So, if the input is like A = 1; B = 5; C = 2, ... Read More

C++ program to calculate how many years needed to get X rupees with 1% interest

Arnab Chakraborty
Updated on 03-Mar-2022 07:37:50

127 Views

Suppose we have a number X. We have 100 rupees in a bank. The bank returns annual interest rate of 1 % compounded annually. (Integers only). We have to check how many years we need to get X rupees?So, if the input is like X = 520, then the output will be 213.StepsTo solve this, we will follow these steps −sum := 0 balance := 100 while balance < n, do:    interest := balance / 100    sum := sum + 1    balance := balance + interest return sumExampleLet us see the following implementation to get better understanding ... Read More

C++ program to find array after removal of left occurrences of duplicate elements

Arnab Chakraborty
Updated on 03-Mar-2022 07:40:37

113 Views

Suppose we have an array A with n elements. We want to remove duplicate elements. We want to leave only the rightmost entry for each element of the array. The relative order of the remaining unique elements should not be changed.So, if the input is like A = [1, 5, 5, 1, 6, 1], then the output will be [5, 6, 1]StepsTo solve this, we will follow these steps −Define two arrays b and vis of size: 1200 each x := 0 n := size of A for initialize i := n - 1, when i >= 0, update (decrease ... Read More

C++ program to find number of distinct values we can get after negating a subarray

Arnab Chakraborty
Updated on 03-Mar-2022 07:33:05

154 Views

Suppose we have an array A with n elements. We select any subset of given numbers and negate these numbers. We have to find maximum number of different values in the array we can get.So, if the input is like A = [1, 1, 2, 2], then the output will be 4, because we can negate first and last numbers to make the array [-1, 1, 2, -2] with four different values.StepsTo solve this, we will follow these steps −Define one set se n := size of A for initialize i := 0, when i < n, update (increase i ... Read More

C++ program to find n valid bracket sequences

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

358 Views

Suppose we have a number n. As we know, a bracket sequence is a string containing only characters "(" and ")". A valid bracket sequence is a bracket sequence which can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. So, if a bracket sequence is like "()()" this is valid because we can put 1's like "(1)+(1)". From number n, we have to find exactly n different possible valid bracket sequences of length 2n.So, if the input is like n = 4, then the output will be ["()()()()", ... Read More

C++ program to find fourth side of a quadrilateral

Arnab Chakraborty
Updated on 03-Mar-2022 07:24:58

180 Views

Suppose we have three numbers a, b and c. We want to make a closed fence in a shape of arbitrary non-degenerate simple quadrilateral. We already have three sides of length a, b and c. We have to find another side d.So, if the input is like a = 12; b = 34; c = 56, then the output will be 42, other answers are also possible.StepsTo solve this, we will follow these steps −return a + b + c - 2ExampleLet us see the following implementation to get better understanding −#include using namespace std; int solve(int a, int ... Read More

C++ program to count expected number of operations needed for all node removal

Arnab Chakraborty
Updated on 03-Mar-2022 07:28:50

93 Views

Suppose we have the adjacency matrix of a directed graph G. Until the graph becomes empty, we are repeating the following operation: Select one vertex from G, then erase that vertex and all vertices that are reachable from that vertex by following some edges. Erasing a vertex will also erase the edges incident to it. We have to find the expected number of times the operation is doneSo, if the input is likethen the output will be 1.6667, because initially select vertex A, remove all vertices, if we select vertex B, remove B and C, and in second operation select ... Read More

C++ program to count number of dodecagons we can make of size d

Arnab Chakraborty
Updated on 03-Mar-2022 07:22:22

88 Views

Suppose we have a number d. Consider there is an infinite number of square tiles and regular triangular tiles with sides length 1. We have to find in how many ways we can form regular dodecagon (12-sided polygon) with sides d using these tiles. If the answer is too large, return result mod 998244353.StepsTo solve this, we will follow these steps−b := floor of d/2 - 1 c := 1 for initialize i := 2, when i < d, update (increase i by 1), do:    b := b * (floor of d/2)    c := c * i return ... Read More

C++ program to find range whose sum is same as n

Arnab Chakraborty
Updated on 03-Mar-2022 07:22:36

142 Views

Suppose we have a number n. We need to find two integers l and r, such that l < r and l + (l + 1) + ... + (r - 1) + r = n.So, if the input is like n = 25, then the output will be l = -2 and r = 7, because (−2) + (−1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25. Other answers are also possible.StepsTo solve this, we will follow these steps −return -(n-1) and nExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int n){    cout

Advertisements