Found 7197 Articles for C++

C++ code to find position of students after coding contest

Arnab Chakraborty
Updated on 15-Mar-2022 06:58:57

379 Views

Suppose we have an array A with n elements. In a coding contest, in total n students will participate, and before the start, every one of them has some positive rating (integer). A[i] represents the rating of ith student. After the contest ends, every student will end up with some positive integer position. We are expecting the students will take places according to their ratings. If student A has rating strictly lower than student B, A will get strictly greater position than B. We have to find the position at the end of the contest.So, if the input is like ... Read More

C++ code to find tree height after n days

Arnab Chakraborty
Updated on 15-Mar-2022 06:56:23

236 Views

Suppose we have an array A with n elements. A has elements either 0 or 1. There is a tree. In consecutive n days, if A[i] is 0 it is not watered, if it is 1 then it is watered, the flower grows in the following manner −If the tree is not watered for consecutive two days, it diesIf the tree is watered on ith day, it grows 1 cmIf the tree is watered on ith and (i+1)th day consecutively, it grows 5 cm instead of 1 cm.If it is not watered on ith day, it will not grow.At the ... Read More

C++ code to find minimum different digits to represent n

Arnab Chakraborty
Updated on 15-Mar-2022 06:50:03

156 Views

Suppose we have a number n. We want to split it into some non-zero digits whose sum is n. We want to find a solution with minimum possible number of different digits.So, if the input is like n = 13, then the output will be [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]StepsTo solve this, we will follow these steps −for initialize i := 0, when i < n, update (increase i by 1), do:    print 1ExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int n){    for (int i = 0; i < n; i++)       printf("1, "); } int main(){    int n = 13;    solve(n); }Input13Output1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

C++ code to check all bulbs can be turned on or not

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

364 Views

Suppose we have a number m and a nested list A with n sub-lists. Consider there are m bulbs, initially all are turned off. There are n buttons and each of them is connected to some set of bulbs. So A[i] is the set of bulbs that can be turned on by pressing ith switch. We have to check whether we can lit up all the bulbs or not.So, if the input is like A = [[1, 4], [1, 3, 1], [2]]; m = 4, then the output will be True, because by pressing all switches we can turn on ... Read More

C++ code to count order collected when client calls

Arnab Chakraborty
Updated on 15-Mar-2022 06:44:25

300 Views

Suppose we have three numbers n, m and z. An office receives calls in every n minutes, and some deliveries come to office in every m minutes. Office is open for z minutes. We have to count the minimum number of orders are collected so there are no pending orders when client calls. Consider taking orders and talking with clients take exactly 1 minutes.So, if the input is like n = 1; m = 2; z = 5, then the output will be 2, because we need to collect orders which comes in second and fourth minutes.StepsTo solve this, we ... Read More

C++ code to count number of lucky numbers with k digits

Arnab Chakraborty
Updated on 15-Mar-2022 06:42:47

985 Views

Suppose we have an array A with n elements, and also another number x. We know the lucky numbers are are positive numbers whose decimal representation only contains lucky digits 4 and 7. Form the given n positive integers. We have to count how many of those have not more than k lucky digits?So, if the input is like A = [44, 74, 474, 154]; k = 2, then the output will be 3, because there are three lucky numbers 44, 74 and 474 but 474 has three lucky digits which is more than k. Also 154 has one lucky ... Read More

C++ code to find xth element after removing numbers

Arnab Chakraborty
Updated on 15-Mar-2022 06:38:35

181 Views

Suppose we have two numbers n and x. First n natural numbers are written on a blackboard. In ith (i starts from 1) operation, we remove ith number from the blackboard. When there are less than i numbers, we stop removal task. We have to find x-th remaining number after stopping removal.So, if the input is like n = 69; x = 6, then the output will be 12. In first operation, i = 1, so remove 1, then in second operation i = 2, but the sequence is 2, 3, 4 ... so second number is 3, remove 3, ... Read More

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

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

213 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

177 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

252 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

Advertisements