Found 7197 Articles for C++

C++ program to concatenate strings in reverse order

Arnab Chakraborty
Updated on 03-Mar-2022 07:52:29

609 Views

Suppose we have two strings S and T. Both are in lowercase letters. Concatenate T and S in this order to generate the final string.So, if the input is like S = "ramming"; T = "prog", then the output will be "programming"StepsTo solve this, we will follow these steps −res := T concatenate S return resExampleLet us see the following implementation to get better understanding −#include using namespace std; string solve(string S, string T){    string res = T + S;    return res; } int main(){    string S = "ramming";    string T = "prog";    cout

C++ program to count number of cities we can visit from each city with given operations

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

718 Views

Suppose we have a list of N coordinate points P in the form (xi, yi). The x and y values are permutation of first N natural numbers. For each k in range 1 to N. We are at city k. We can apply the operations arbitrarily many times. The operation: We move to another city that has a smaller x-coordinate and a smaller y-coordinate or larger x or larger y coordinate than the city we are currently in. We have to find the number of cities we can reach from city k.So, if the input is like P = [[1, ... Read More

C++ program to find order of loading golds on weight scale without exploding it

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

143 Views

Suppose we have an array A with n distinct elements, and another number x. There are n pieces of gold. The ith gold weight is A[i]. We will put this n pieces on weight scale one piece at a time. But the scale has an unusual defect: if the total weight on it is exactly x, it will explode. We have to check whether we can put all n gold pieces onto the scale in some order, without exploding the scale during the process. If we can, find that order. If not possible, mark "IMPOSSIBLE".So, if the input is like ... Read More

C++ program to find maximum distance between two rival students after x swaps

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

306 Views

Suppose we have four numbers n, x, a and b. There are n students in the row. There are two rivalling students among them. One of them is at position a and another one is at position b. Positions are numbered from 1 to n from left to right. We want to maximize the distance between these two students. We can perform the following operation x times: Select two adjacent students and then swap them. We have to find the maximum possible distance after x swaps.So, if the input is like n = 5; x = 1; a = 3; ... Read More

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

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

187 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

235 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

176 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

155 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

223 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

469 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

Advertisements