Found 7197 Articles for C++

C++ code to find how long person will alive between presses

Arnab Chakraborty
Updated on 30-Mar-2022 12:33:35

110 Views

Suppose we have four numbers d, L, v1 and v2. Two presses are initially at location 0 and L, they are moving towards each other with speed v1 and v2 each. The width of a person is d, he dies if the gap between two presses is less than d. We have to find how long the person will stay alive.So, if the input is like d = 1; L = 9; v1 = 1; v2 = 2;, then the output will be 2.6667StepsTo solve this, we will follow these steps −e := (L - d)/(v1 + v2) return eExampleLet ... Read More

C++ code to find minimum jump to reach home by frog

Arnab Chakraborty
Updated on 30-Mar-2022 12:30:48

413 Views

Suppose we have one binary string S with n bits and another number d. On a number line, a frog wants to reach point n, starting from the point 1. The frog can jump to the right at a distance not more than d. For each point from 1 to n if there is a lily flower it is marked as 1, and 0 if not. The frog can jump only in points with a lilies. We have to find the minimal number of jumps that the frog needs to reach n. If not possible, return -1.So, if the input ... Read More

C++ code to count maximum groups can be made

Arnab Chakraborty
Updated on 30-Mar-2022 12:27:18

402 Views

Suppose we have an array A with n elements. There were n groups of students. A group is either one person who can write the code with anyone else, or two people who want to write the code in the same team. But the mentor decided to form teams of exactly three people. We have to find the maximum number of teams of three people the mentor can form. For groups of two, either both students should write the code, or both should not. If two students from a group of two will write the code, they should be in ... Read More

C++ code to count days to complete reading book

Arnab Chakraborty
Updated on 29-Mar-2022 12:18:41

318 Views

Suppose we have an array A with n elements and have another value t. On ith day Amal spends A[i] seconds in work. In the free time he reads a book. The entire book will take t seconds to complete. We have to find how many days he will need to read the complete book.So, if the input is like A = [86400, 86398]; t = 2, then the output will be 2, because one day has 86400 seconds, and first day is totally blocked. On second day he will get 2 seconds to complete the book.StepsTo solve this, we ... Read More

C++ code to count local extrema of given array

Riya Kumari
Updated on 05-Jan-2024 16:17:29

860 Views

Extrema are such numbers which are either minima or maxima. In other words, it is a number or element which is either greater than or less than both of its adjacent values. Suppose we have an array A with n elements. An element of this array A[i] is called a local minimum if and only if it is strictly less than both of its neighbours. Also if it is strictly greater than its neighbours it will be local maximum. For A[0] and A[n-1] as there are only one neighbour they are not maxima or minima. We have to find the ... Read More

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

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

253 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

272 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

463 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

291 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

157 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

Advertisements