Server Side Programming Articles - Page 527 of 2650

C++ code to find maximum stones we can pick from three heaps

Arnab Chakraborty
Updated on 15-Mar-2022 06:36:24

232 Views

Suppose we have three numbers a, b and c. There are three heaps of stones with a, b, and c number of stones respectively. Each time we can do these operations −Take one stone from the first heap and two stones from the second heap (when the heaps have necessary number of stones)Take one stone from the second heap and two stones from the third heap (when the heaps have necessary number of stones)We have to count maximum how many stones we can collect?So, if the input is like a = 3; b = 4; c = 5, then the ... Read More

C++ code to find number to disprove given prime hypothesis

Arnab Chakraborty
Updated on 15-Mar-2022 06:34:12

190 Views

Suppose we have a number n. Let there is a hypothesis "There exists a positive integer n that for each positive integer m number (n·m + 1) is a prime number". We have to find such m as a counter example to disprove this statement.So, if the input is like n = 12, then the output will be 10, because 12*10 + 1 = 121 which is not prime.StepsTo solve this, we will follow these steps −if n < 3, then:    return n + 2 Otherwise    return n - 2ExampleLet us see the following implementation to get better ... Read More

C++ code to get two numbers in range x with given rules

Arnab Chakraborty
Updated on 15-Mar-2022 06:32:01

263 Views

Suppose we have a number x. We have to find two integers a and b, such that both of them will be in between 1 and x, a is divisible by b, a * b > x but a/b < x. If not possible, return -1. So, if the input is like x = 10, then the output will be 6 and 3, (other answers are also possible)To solve this, we will follow these steps −if x < 2, then:    print -1    return print x and xExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int x){    if (x < 2){       cout

C++ code to check review vote status and uncertainty

Arnab Chakraborty
Updated on 15-Mar-2022 06:29:49

513 Views

Suppose we have three numbers x, y and z. On a review site there were x persons who would upvote, y persons who would downvote, and another group of z persons who would vote, but we do not know whether they would upvote or downvote. Each person can vote at most once. If there are more people upvote than downvote, the result will be "+"; if downvote count is greater, the result will be "-"; otherwise the result will be "0". Because of the z unknown persons, the result may be uncertain (i.e. there are more than one possible results). ... Read More

C++ code to count years to reach certain rank in an army

Arnab Chakraborty
Updated on 15-Mar-2022 06:27:22

243 Views

Suppose we have an array D with n-1 elements and two values a and b. In an army, there are n ranks numbered from 1 to n. One needs D[i] years to rise from rank i to rank i+1. Amal has just reached new rank 'a' but he wants to reach rank 'b'. We have to count the number of years he will need to reach his goal.So, if the input is like D = [5, 6]; a = 1; b = 3, then the output will be 11.To solve this, we will follow these steps −n := size of ... Read More

C++ code to count number of unread chapters

Arnab Chakraborty
Updated on 15-Mar-2022 06:24:55

191 Views

Suppose we have an array of pairs P. Where P[i] is in the form (l, r), and have another number k. Consider we are going to read a book with n chapters. so that one page of the book belongs to exactly one chapter and each chapter contains at least one page. We have read some pages and marked the page with number k as the first page which was not read. We have to find the number of chapters we have not completely read yet. P[i] represents the chapter page numbers range.So, if the input is like P = ... Read More

C++ code to find sorted array with non-divisibility conditions

Arnab Chakraborty
Updated on 15-Mar-2022 06:22:46

128 Views

Suppose we have a number n. Consider we are going to form an array A with n elements. A is sorted in ascending order and all elements are distinct. For every i from 2 to n (considering array index starts from 1) A[i] is not divisible by A[i-1].So, if the input is like n = 7, then the output will be [2, 3, 4, 5, 6, 7, 8]To solve this, we will follow these steps −for initialize i := 2, when i

C++ code to find minimum difference between concerts durations

Arnab Chakraborty
Updated on 15-Mar-2022 06:19:00

282 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
Updated on 15-Mar-2022 06:17:15

184 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 count operations to make array sorted

Arnab Chakraborty
Updated on 15-Mar-2022 05:59:01

351 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

Advertisements