Found 7347 Articles for C++

C++ code to check reengagements can be done so elements sum is at most x

Arnab Chakraborty
Updated on 11-Mar-2022 06:58:23

61 Views

Suppose we have two arrays A and B of size n, and another number x. We have to check whether we can rearrange the elements in B, so that A[i] + B[1]

C++ code to find composite numbers whose difference is n

Arnab Chakraborty
Updated on 11-Mar-2022 06:54:57

213 Views

Suppose we have a number n. We have to find two composite integers (non-prime) a and b, such that a - b = n.So, if the input is like n = 512, then the output will be 4608 and 4096StepsTo solve this, we will follow these steps −print 10*n and 9*n.ExampleLet us see the following implementation to get better understanding −#include using namespace std; void solve(int n){    cout

C++ code to find maximum score we can assign to first student

Arnab Chakraborty
Updated on 11-Mar-2022 06:52:42

438 Views

Suppose we have an array A with n elements and a number m. There are n students giving an exam. The highest possible score is m. A[i] is the score of ith student. We can manipulate each students score, but the conditions must be satisfied. The score will not exceed m, all scores are integers and the average marks of all student does not change. If we want to maximize the first person's score what will be the highest possible score we can give.So, if the input is like A = [1, 2, 3, 4]; m = 10, then the ... Read More

C++ code to find minimum arithmetic mean deviation

Arnab Chakraborty
Updated on 11-Mar-2022 06:50:12

167 Views

Suppose we have an array A with 3 elements. A[1] is arithmetic mean of two elements A[0] and A[3] if A[0] + A[2] = 2 * A[1]. Arithmetic mean deviation of three numbers d(A[0], A[1], A[2]) is |A[0] + A[2] - 2*A[1]|. We can perform following operations any number of times: Select two index i and j from the indices {0, 1, 2} such that i != j, then increase A[i] by 1 and decrease A[j] by 1. We have to find the minimum value of arithmetic mean deviation.So, if the input is like A = [2, 2, 6], then ... Read More

C++ code to find the number of dial rotations to print a string

Arnab Chakraborty
Updated on 11-Mar-2022 06:40:32

117 Views

Suppose, we are given a rotary dial that contains all lowercase English alphabets. There is a printer attached to the dial and whichever character resides in the pointer of the rotary dial for 3 seconds, gets printed. The rotary dial initially stays at the letter 'a' and it does not reset to the initial position whenever it prints a character. We are given a string s and we have to print the given string. Whenever we move the dial to another letter, one amount of rotation takes place. We have to find out the total amount of rotations needed to ... Read More

C++ code to find the lamps needed to light up a floor

Arnab Chakraborty
Updated on 11-Mar-2022 06:38:37

301 Views

Suppose, there is a floor divided into a grid that has n rows and m columns. Now the floor has to be lit using lamps. A lamp, if placed at the border of two cells can light up two cells. If the lamp is placed in the vertical border, it lights up the cells to its left and right and if it is placed in the horizontal border, it lights up cells to its front and back. Given n and m, we have to find out the minimum number of lamps needed to light up the whole floor.So, if the ... Read More

C++ code to find out if an image is B/W or color

Arnab Chakraborty
Updated on 11-Mar-2022 06:34:31

1K+ Views

Suppose, we are given an image that contains n pixels. The pixels can be of the following color −'C' (cyan)'M' (magenta)'Y' (yellow)'W' (white)'G' (grey)'B' (black)The color of the i-th pixel is given in the string 'pixels'. Given the string, we have to find out if the given photograph is colorful or black and white. If it is a color photograph it will contain at least one pixel of color 'C', 'M' and 'Y' and we will print 'Color'; otherwise, it will contain pixels of color 'W', 'G', 'B' only and we will print 'BW'.So, if the input is like n ... Read More

C++ code to find out the total amount of sales we made

Arnab Chakraborty
Updated on 11-Mar-2022 06:29:34

633 Views

Suppose, we are selling 4 items and the price of the i-th item is given in the array 'cost[i]'. Now we sell the items in the order given in the string 'items'. We have to find out the total amount of sales that we have made. The string 'items' contain integer numbers from 1 to 4, duplicates can be present and they can be in any order.So, if the input is like cost = {10, 15, 10, 5}, items = "14214331", then the output will be 75.StepsTo solve this, we will follow these steps −total := 0 for initialize i ... Read More

C++ code to find total number of digits in special numbers

Arnab Chakraborty
Updated on 11-Mar-2022 05:58:58

708 Views

Suppose, we are given an integer number k. We call a number special number if all the digits in that number are the same. For example, 1, 11, 1111 are special numbers. We count the special numbers in order 1, 11, 111, 1111, 2, 22, 222, 2222, 3, 33, 333, 3333, and so on. We have to find out the total number of digits that are in special numbers up to k. The value of k is not greater than 10000.So, if the input is like k = 9999, then the output will be 90.StepsTo solve this, we will follow ... Read More

C++ code to find out the sum of the special matrix elements

Arnab Chakraborty
Updated on 11-Mar-2022 05:56:21

369 Views

Suppose, we are given a square matrix of dimensions n * n. The following values of the matrix are called special elements −Values that are in the main diagonal.Values that are in the second diagonal.Values of the row that has exactly (n - 1 / 2) rows above it and the same number of rows below it.Values of the column that has exactly (n - 1 / 2) columns at its left and right.We find out the sum of these special values in the matrix.So, if the input is like n = 4, mat = {{1, 2, 3, 4}, {5, ... Read More

Advertisements