Found 7347 Articles for C++

C++ code to find palindrome string whose substring is S

Arnab Chakraborty
Updated on 29-Mar-2022 12:01:05

199 Views

Suppose we have a string S with n letters. We have to find another string T, such that T is palindrome and S is subsequence of T.So, if the input is like S = "ab", then the output will be "aabaa" (other answers are also available)StepsTo solve this, we will follow these steps −res := S reverse the array S res := res + S return resExampleLet us see the following implementation to get better understanding −#include using namespace std; string solve(string S){    string res = S;    reverse(S.begin(), S.end());    res += S;    return res; } ... Read More

C++ code to find reduced direction string of robot movement

Arnab Chakraborty
Updated on 29-Mar-2022 11:59:08

176 Views

Suppose we have a string S with n letters. The letters are either 'R' or 'U'. On a 2D plane, a robot can go right or up. When it is 'R' it moves right and when it is 'U' it moves up. However the string is too large, we want to make the string smaller. A pair like "RU" or "UR" will be replaced as diagonal move "D". We have to find the length of the final updated reduced string.So, if the input is like S = "RUURU", then the output will be 5, because the string will be "DUD"StepsTo ... Read More

C++ code to get shortest distance from circular stations

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

336 Views

Suppose we have two numbers s and t, and another array D with n elements. The circle line of the Dreamland subway has n different stations. We know the distances between all pairs of neighboring stations: D[i] is the distance between station i and i+1, and D[n-1] is the distance between (n-1) and 0th station. We have to find shortest distance from s to t.So, if the input is like s = 1; t = 3; D = [2, 3, 4, 9], then the output will be 5.StepsTo solve this, we will follow these steps −n := size of D ... Read More

C++ code to check given flag is stripped or not

Arnab Chakraborty
Updated on 29-Mar-2022 11:53:56

230 Views

Suppose we have a matrix of size n x m. Each cell will hold one value from 0 to 9. There is a flag should be striped: each horizontal row of the flag should contain squares of the same color, and the colors of the adjacent horizontal rows should be different. We have to check given matrix is valid flag or not.So, if the input is like000111333StepsTo solve this, we will follow these steps −n := row count of matrix m := column count of matrix l := 'm' res := 1 for initialize i := 0, when i < ... Read More

C++ code to find minimum correct string from given binary string

Arnab Chakraborty
Updated on 29-Mar-2022 11:49:30

94 Views

Suppose we have a binary string S with n bits. There are no redundant leading zeroes. We can perform two different operations on S −Swap any pair of adjacent bitsReplace all "11" with "1"Let val(S) is decimal representation of S. We have to find minimum correct string, where correct string A is less than another correct string 'B' when val(A) < val(B)So, if the input is like S = "1001", then the output will be 100, because we can perform the operation like "1001" -> "1010" -> "1100" -> "100".StepsTo solve this, we will follow these steps −n := size ... Read More

C++ code to find answers by vowel checking

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

145 Views

Suppose we have a string S. Amal and Bimal are playing a game. The rules of the game are like: Those who play for the first time, that is Amal is the detective, he should investigate a "crime" and find out the cause. He can ask any questions whose answers will be either "Yes" or "No". If the question’s last letter is a vowel, they answer "Yes" otherwise "No". Here the vowels are: A, E, I, O, U, Y. We have S as question and we have to find the answer.So, if the input is like S = "Is it ... Read More

C++ code to find center of inner box

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

297 Views

Suppose we have a matrix of size n x m. Cells are either 'W' as white or 'B' as black. Some square inside the table with odd length was painted black. We have to find the center of this square.So, if the input is likeWWBBBWWWBBBWWWBBBWWWWWWWWWWWWWthen the output will be (3, 1).StepsTo solve this, we will follow these steps −n := row count of matrix m := column count of matrix cnt := 0 X := 0 Y := 0 for initialize i := 0, when i < n, update (increase i by 1), do:    for initialize j := 0, ... Read More

C++ code to find greater number whose factor is k

Arnab Chakraborty
Updated on 29-Mar-2022 11:37:21

128 Views

Suppose we have two numbers n and k. We have to find smallest integer x which is greater than n and divisible by k.So, if the input is like n = 5; k = 3, then the output will be 6.StepsTo solve this, we will follow these steps −return n + k - (n mod k)ExampleLet us see the following implementation to get better understanding −#include using namespace std; int solve(int n, int k){    return n + k - n % k; } int main(){    int n = 5;    int k = 3;    cout

C++ code to check pile count is valid from second day

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

121 Views

Suppose we have two arrays X and Y of same size. There are stone piles with X[i] number of stones on ith index on the first day and on the second day ith index has Y[i] number of stones. On the first day many members have come. Either they do nothing or they add few stones to some pile or they swap few stones from one pile to another. We have to check whether Y is valid from X or not.So, if the input is like X = [1, 2, 3, 4, 5]; Y = [2, 1, 4, 3, 5], ... Read More

C++ code to find minimum k to get more votes from students

Arnab Chakraborty
Updated on 29-Mar-2022 11:33:28

167 Views

Suppose we have an array A with n elements. There are n students in a school and each of them has exactly k votes and all votes should be used. There are two parties. The A[i] represents ith student has given A[i] amount of votes to first party and this implies the second party will get k- A[i] number of votes. The second party wants to set k in such a way that they win. What will be the minimum possible value of k.So, if the input is like A = [2, 2, 3, 2, 2], then the output will ... Read More

Advertisements