C++ Articles

Page 458 of 597

C++ code to find minimum difference between concerts durations

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 356 Views

Suppose we have three numbers a, b and c. A singer has 'a' one-minute songs, 'b' tow-minutes song and 'c' three-minutes song. He wants to distribute all songs into two concerts, such that every song should be included to exactly one concert. He wants to make the absolute difference of durations of the concerts as small as possible. The duration of the concert is the sum of durations of all songs in that concert. We have to find the minimal possible difference between the concerts durations.So, if the input is like a = 2; b = 1; c = 3, ...

Read More

C++ code to count number of even substrings of numeric string

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 210 Views

Suppose we have a string S with n digits. A substring of S is said to be even if the number represented by this string is also even. We have to find the number of even substrings of S.So, if the input is like S = "1234", then the output will be 6, because the substrings are 2, 4, 12, 34, 234, 1234.To solve this, we will follow these steps −a := 0 n := size of S for initialize i := 0, when i < n, update (increase i by 1), do:    if S[i] mod 2 is same ...

Read More

C++ code to find string where trygub is not a substring

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 246 Views

Suppose we have a string S with n lowercase English letters. We have to reorder the characters in S, so that "trygub" is not a subsequence of the resulting string.So, if the input is like S = "pintontrygubabc", then the output will be "abbcginnoprttuy".StepsTo solve this, we will follow these steps −sort the array S return SExampleLet us see the following implementation to get better understanding −#include using namespace std; string solve(string S){    sort(S.begin(), S.end());    return S; } int main(){    string S = "pintontrygubabc";    cout

Read More

C++ code to find three numbers whose sum is n

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 455 Views

Suppose we have a number n. We are going to find three numbers a, b and c, such that a + b + c = n and none of these three numbers are multiple of 3.So, if the input is like n = 233, then the output will be [77, 77, 79]StepsTo solve this, we will follow these steps −if (n - 2) mod 3 is same as 0, then:    return 1, 2, and n - 3 Otherwise    return 1, 1, and n - 2ExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int n){    if ((n - 2) % 3 == 0)       cout

Read More

C++ code to count operations to make array sorted

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 392 Views

Suppose we have an array A with n elements (n is odd). A contains a permutation of first n natural numbers. Let there is a function f(i) this takes single argument i in range 0 to n-2, and does the operation: if A[i] > A[i+1], swap the values of A[i] and A[i+1]. We have to count number of iterations to make array A sorted, for the first time.So, if the input is like A = [4, 5, 7, 1, 3, 2, 6], then the output will be 5, because the array states after each iteration is like: [4, 5, 1, ...

Read More

C++ code to count minimum button click to set volume as before

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 274 Views

Suppose we have two numbers a and b. Amal always sets TV volume to 'b' value. But someday Bimal has changed it to 'a' value. The remote has six buttons (-5, -2, -1, 1, 2, 5) using them we can increase or decrease the volume by 1, 2 or 5. Volume can be very large but not negative. We have to count the number of buttons Amal needs to press at minimum to get the volume same as b.So, if the input is like a = 5; b = 14, then the output will be 3, because press +5 to ...

Read More

C++ code to kid chair association

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 375 Views

Suppose we have a number n. We have to find an array A of size n. There are n tables and each table has 4 chairs. Chairs are numbered from 1 to 4n. It is known that two kids who sit on chairs with numbers a and b (a != b) will indulge if −gcd(a, b) = 1 or, a divides b or b divides a.We want to seat the kids so there are no 2 of the kid that can indulge. More formally. We have to find the chair association.So, if the input is like n = 4, then ...

Read More

C++ code to find total time to pull the box by rabbit

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 385 Views

Suppose we have two coordinates (x1, y1) and (x2, y2). A rabbit is pulling food box. He is attached with a rope with only 1 unit sized rope. Rabbit will pull the box to where it is standing before moving out of the way in the same direction by 1 unit. Rabbit can move 1 unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for it to be in exactly 1 unit away from the box. If he wants to pull the box again, it must go to a point ...

Read More

C++ code to count number of notebooks to make n origamis

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 287 Views

Suppose we have two numbers n and k. In a party there are n invited friends. Amal wants to make invitations in the form of origami. For each invitation, he needs two red papers, five green papers, and eight blue papers. There are infinite number of notebooks of each color, but each notebook consists of only one color with k papers. We have to find the minimum number of notebooks that Amal needs to buy to invite all n of his friends.So, if the input is like n = 3; k = 5, then the output will be 10, because ...

Read More

C++ code to find two substrings with one minimal substring

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2022 284 Views

Suppose we have a lowercase string S with n characters. We have to find two non-empty substrings P and Q, such that −Both P and Q are subsequences of SFor each index i, S[i] belong to exactly one of P and Q.P is lexicographically minimum as possible.So, if the input is like S = "thelightsaber", then the output will be 10, because we need 2 red notebooks, 3 green notebooks, and 5 blue notebooks.StepsTo solve this, we will follow these steps −c := S sort the array c a := position of (c[0]) in S delete c from S print ...

Read More
Showing 4571–4580 of 5,961 articles
« Prev 1 456 457 458 459 460 597 Next »
Advertisements