Found 7347 Articles for C++

C++ code to find minimum moves with weapons to kill enemy

Arnab Chakraborty
Updated on 15-Mar-2022 04:58:48

445 Views

Suppose we have an array A with n elements, and another number H. H is health of an enemy. We have n weapons and damaging power of ith weapon is A[i]. Different weapons can be used to kill the enemy. We cannot use same weapon twice in a row. We have to count minimum how many times we can use weapons to kill the enemy.So, if the input is like A = [2, 1, 7]; H = 11, then the output will be 3, because if we use weapon with damage power 7, then use 2 then again use 7 ... Read More

C++ code to get updated string with same 'a' and 'b' count

Arnab Chakraborty
Updated on 15-Mar-2022 04:55:38

124 Views

Suppose we have as string S with even length n. S contains only two types of characters 'a' and 'b'. We want to modify the string so that every its prefix of its length has an equal amount of letters 'a' and 'b'. To achieve that, we can perform the following operation arbitrary number of times: Select some position in his string and replace the letter on this position with the other letter. Return the updated string.So, if the input is like S = "aabbbb", then the output will be "baabab"StepsTo solve this, we will follow these steps −n := ... Read More

C++ code to find card spread way to make all sum equal for each player

Arnab Chakraborty
Updated on 15-Mar-2022 04:51:50

106 Views

Suppose we have an array A with n elements. Here n is even. A[i] is a number written on ith card. There are n/2 people who want to play a game. At the beginning, each player will take two cards. We have to find the way to distribute cards in such a way that sum of values written on the cards will be same for each player.So, if the input is like A = [1, 5, 7, 4, 4, 3], then the output will be [(0, 2), (5, 1), (3, 4)], because A[0] + A[2] = 8, A[5] + A[1] ... Read More

C++ code to count number of operations to make two arrays same

Arnab Chakraborty
Updated on 15-Mar-2022 04:46:15

173 Views

Suppose we have two arrays A and B with n number of elements. Consider an operation: Select two indices i and j, then decrease ith element by 1 and increase jth element by 1. Each element of the array must be non-negative after performing an operation. We want to make A and B same. We have to find the sequence of operations to make A and B same. If not possible, return -1.So, if the input is like A = [1, 2, 3, 4]; B = [3, 1, 2, 4], then the output will be [(1, 0), (2, 0)], because ... Read More

C++ code to find minimum operations to make numbers c and d

Arnab Chakraborty
Updated on 11-Mar-2022 07:20:07

224 Views

Suppose we have two numbers c and d. Amal has two numbers a and b initially both are zero. Amal wants to perform some operation on them. Before performing each operation, some positive integer k is picked, which is then used to perform one of the following operations −add number k to both a and b, oradd number k to a and subtract k from b, oradd number k to b and subtract k from a.We have to find the minimum number of operations needed to make a and b equal to c and d respectively. If not possible, return ... Read More

C++ code to check phone number can be formed from numeric string

Arnab Chakraborty
Updated on 11-Mar-2022 07:17:41

745 Views

Suppose we have a string S with n digits. A number with exactly 11 digits is a telephone number if it starts with '8'. In one operation, we can remove one digit from S. We have to check whether we can make the string a valid phone number or not.So, if the input is like S = "5818005553985", then the output will be True, because we can make the string "8005553985" with 11 characters and first digit is 8.StepsTo solve this, we will follow these steps −m := size of S insert '8' at the end of S if if location of 8

C++ code to process query operation on binary array

Arnab Chakraborty
Updated on 11-Mar-2022 07:15:51

225 Views

Suppose we have an array A with n elements and another list of queries Q with q queries. each Query[i] contains a pair (x, k). When we process a query, for x: decrease the value of A[x] by 1. For k, print kth largest element. Initially all elements in A is either 0 or 1.So, if the input is like A = [1, 1, 0, 1, 0]; Q = [[2, 3], [1, 2], [2, 3], [2, 1], [2, 5]], then the output will be [1, 1, 1, 0]StepsTo solve this, we will follow these steps −n := size of A ... Read More

C++ code to check pack size can be determined from given range

Arnab Chakraborty
Updated on 11-Mar-2022 07:06:35

99 Views

Suppose we have two numbers l and r. There is a shop and we want to sell some food container with 'a' number of foods with a discount, and some customer wants to buy x foods. The customer following a greedy strategy −He buys floor of (x/a) packs with discountThen wants to buy remaining (x mod a) foods one by one.But the customer is greedy, so if he wants to buy (x mod a) foods one by one and it happens that (x mod a) ≥ a/2, so he decides to buy the whole pack of a foods. A customer ... Read More

C++ code to count copy operations without exceeding k

Arnab Chakraborty
Updated on 11-Mar-2022 07:04:35

145 Views

Suppose we have an array A with n elements and another number k. There are n piles of candies. The ith pile has A[i] number of candies. We can perform the operation on two indices i and j (i != j), then add another A[i] number of candies to A[i] (A[i] will not be reduced). We can perform this operation any number of times, but unfortunately if some pile contains strictly more than k candies we cannot perform the operation anymore. We have to find the maximum number of times we can perform this operation.So, if the input is like ... Read More

C++ code to check array can be formed from Equal Not-Equal sequence or not

Arnab Chakraborty
Updated on 11-Mar-2022 07:02:20

123 Views

Suppose we have a string S of length . Consider there are n numbers and they are arranged in a circle. We do not know the values of these numbers but if S[i] = 'E' it indicates ith and (i+1)th numbers are same, but if that is 'N' then they are different. From S we have to check whether we can recreate the sequence or not.So, if the input is like S = "ENNEENE", then the output will be True, because we can assign values like [15, 15, 4, 20, 20, 20, 15].StepsTo solve this, we will follow these steps ... Read More

Advertisements