Server Side Programming Articles - Page 531 of 2650

C++ code to find minimum arithmetic mean deviation

Ravi Ranjan
Updated on 31-Jul-2025 12:03:45

290 Views

The arithmetic mean deviation or mean absolute deviation is the summation of the absolute differences of each element and the mean of the dataset, then dividing it by the total number of data elements. To calculate the minimum arithmetic mean deviation, we used the median of the dataset in place of the mean. The formula to calculate the minimum arithmetic mean deviation is given as: $$ \text{MMD} = \frac{1}{n} \sum_{i=1}^{n} |x_i - M| = \frac{|x_1 - M| + |x_2 - M| + \cdots + |x_n - M|}{n} $$ Here are some example scenarios to calculate the ... 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

204 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

474 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

2K+ 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

811 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

916 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

517 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

C++ code to find the area taken by boxes in a container

Arnab Chakraborty
Updated on 11-Mar-2022 05:52:30

222 Views

Suppose, we have n pairs of boxes that need to be shipped in a square-shaped container. The width of the pair of the boxes is given as a pair (a, b) and they are given in an array 'dimensions'. If we keep the width of the boxes parallel to each other, we have to find out how much area the boxes will take inside the container. We cannot stack the boxes on top of each other. We determine the minimum area required by the two boxes in the container for all the n pairs.So, if the input is like n ... Read More

C++ code to find out who won an n-round game

Arnab Chakraborty
Updated on 11-Mar-2022 05:49:01

425 Views

Suppose, there is a two-player game that has n rounds. The scores of the rounds are given in an array 'scores' where each element is of the format {P1 Score, P2 Score}. The player with the higher score wins a round, and a player wins the game if they have won more rounds; otherwise, it is declared as a draw. So, given the scores, we have to find out who has won the game.So, if the input is like n = 4, scores = {{4, 3}, {3, 2}, {5, 6}, {2, 5}}, then the output will be Draw.StepsTo solve this, ... Read More

C++ code to find out number of battery combos

Arnab Chakraborty
Updated on 11-Mar-2022 05:45:04

350 Views

Suppose, we have n batteries that can be used a maximum of 5 times. We have some devices that need three batteries and each usage of the device increases the usage count of the batteries by 1. If we have to use the devices k times, we have to find out how many battery combinations we can make to power the devices. A battery cannot be used in two devices simultaneously and a battery that has been used 5 times cannot be included. The usage count of the batteries is given in the array batt.So, if the input is like ... Read More

Advertisements