Found 7347 Articles for C++

C++ program to check three items circularly likes next item or not

Arnab Chakraborty
Updated on 03-Mar-2022 10:51:51

53 Views

Suppose we have an array A with n elements. There are n planes on Earth and they are numbered from 1 to n. The plane with number i likes plane A[i]. A[i] != i. We have to check whether there are three planes p, q, and r where p likes q, q likes r and r likes p.So, if the input is like A = [2, 4, 5, 1, 3], then the output will be True, because the triplet is [2, 4, 1].StepsTo solve this, we will follow these steps −n := size of A for initialize i := 0, ... Read More

C++ program to find perfect array of size n whose subarray is a good array

Arnab Chakraborty
Updated on 03-Mar-2022 10:48:51

144 Views

Suppose we have a number n. An array B is good if the sum of its elements is divisible by the length of this array. We can say an array A with n elements is perfect, if non-empty subarray of this array A is good and elements in A is in range 1 to 100. From the number n, we have to find an array A which is perfect.So, if the input is like n = 4, then the output will be [7, 37, 79, 49], other answers are also possible.StepsTo solve this, we will follow these steps −for initialize ... Read More

C++ program to count number of problems can be solved from left or right end of list

Arnab Chakraborty
Updated on 03-Mar-2022 10:43:31

210 Views

Suppose we have an array A with n elements and another number k is there. Consider there are n problems on a contest. Amal's problem solving skill is k. Amal always solves problem from any of the ends of a list. And He cannot solve problem whose difficulty is greater than k. He stops when the left and right problems' difficulty is greater than k. We have to count how many problems he can solve. A[i] represents the difficulty of ith problem.So, if the input is like A = [4, 2, 3, 1, 5, 1, 6, 4]; k = 4, ... Read More

C++ program to find minimum how many coins needed to buy binary string

Arnab Chakraborty
Updated on 03-Mar-2022 10:39:43

126 Views

Suppose we have three numbers c0, c1 and h, and a binary string S. We can flip any bit in S. We should pay h coins for each change. After some changes (possibly zero) we want to buy the string. To buy the string we should buy all its characters. To buy the bit 0 we should pay c0 coins, to buy the bit 1 you should pay c1 coins. We have to find the minimum number of coins needed to buy the string.So, if the input is like c0 = 10; c1 = 100; h = 1; S = ... Read More

C++ program to count how many ways two players win or make draw in dice throwing game

Arnab Chakraborty
Updated on 03-Mar-2022 10:34:33

168 Views

Suppose we have two numbers a and b. Amal and Bimal are playing a game. First each of them writes an integer from 1 to 6, then a dice is thrown. The player whose written number got closer to the number written on the paper, he wins that round, if both of them has the same difference, then that is a draw. If Amal writes the number a, and Bimal writes b, then we have to count the number of possible ways the Amal will win, number of possible draw and number of ways Bimal can win.So, if the input ... Read More

C++ program to count minimum number of binary digit numbers needed to represent n

Arnab Chakraborty
Updated on 03-Mar-2022 10:30:18

245 Views

Suppose we have a number n. A number is a binary decimal if it's a positive integer and all digits in its decimal notation are either 0 or 1. For example, 1001 (one thousand and one) is a binary decimal, while 1021 are not. From the number n, we have to represent n as a sum of some (not necessarily distinct) binary decimals. Then compute the smallest number of binary decimals required for that.So, if the input is like n = 121, then the output will be 2, because this can be represented as 110 + 11 or 111 + ... Read More

C++ program to find sequence of indices of the team members

Arnab Chakraborty
Updated on 03-Mar-2022 10:27:04

126 Views

Suppose we have an array A with n elements and a number k. There are n students in a class. The rating of ith student is A[i]. We have to form a team with k students such that rating of all team members are distinct. If not possible return "Impossible", otherwise return the sequence of indices.So, if the input is like A = [15, 13, 15, 15, 12]; k = 3, then the output will be [1, 2, 5].StepsTo solve this, we will follow these steps −Define two large arrays app and ans and fill them with cnt := 0 ... Read More

C++ program to count minimum number of operations needed to make number n to 1

Arnab Chakraborty
Updated on 03-Mar-2022 10:20:34

289 Views

Suppose we have a number n. We perform any one of these operations arbitrary number of times −Replace n with n/2 when n is divisible by 2Replace n with 2n/3 when n is divisible by 3Replace n with 4n/5 when n is divisible by 5We have to count minimum number of moves needed to make number 1. If not possible, return -1.So, if the input is like n = 10, then the output will be 4, because use n/2 to get 5, then 4n/5 to get 4, then n/2 again to get 2 and n/2 again to get 1.StepsTo solve ... Read More

C++ program to find reduced size of the array after removal operations

Arnab Chakraborty
Updated on 03-Mar-2022 10:13:13

111 Views

Suppose we have an array A with n elements. Consider there is a password with n positive integers. We apply the following operations on the array. The operations is to remove two adjacent elements that are not same as each other, then put their sum at that location. So this operation will reduce the size of array by 1. We have to find the shortest possible length of the array after performing these operations.So, if the input is like A = [2, 1, 3, 1], then the output will be 1, because if we select (1, 3), the array will ... Read More

C++ program to find minimum how many operations needed to make number 0

Arnab Chakraborty
Updated on 03-Mar-2022 10:09:06

204 Views

Suppose we have a numeric string S with n digits. Consider S represents a digital clock and whole string shows an integer from 0 to 10^n - 1. If there are a smaller number of digits, it will show leading 0s. Follow the operations −Decrease the number on clock by 1, orSwap two digitsWe want the clock will show 0 with minimum number of operations needed. We have to count the number of operations needed to do that.So, if the input is like S = "1000", then the output will be 2, because we can swap first 1 with last ... Read More

Advertisements