Found 7197 Articles for C++

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

799 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

904 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

505 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

210 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

414 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

341 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

Partition Problem in C++

Prateek Jangid
Updated on 07-Mar-2022 09:29:48

718 Views

In this problem, we must build C++ code to determine whether or not an array may be divided into two equal subarrays. Also, we have to check the condition if the sum of all the elements in both subarrays is exactly the same or not. The partitioning problem is a variant of the Subset Sum Problem, which is in turn a variant of the Knapsack Problem. We'll use the C++ programming language to tackle the partition problem. We must return a string with a Yes or No depending on whether the specified condition is fulfilled or not.Inputarr[] = {6, 4, ... Read More

Parallel Array in C++

Prateek Jangid
Updated on 07-Mar-2022 08:14:09

2K+ Views

The parallel array is also called the structure array.Definition − A parallel array can be defined as multiple arrays in which the ith elements are closely related, and together, they constitute an entity. An array is a fundamental feature in the C++ language. Making parallel arrays helps us in comparing two or more arrays.For instance, first_name = ['John', 'Dexter', 'Fredd', 'Hank', 'james'] last_name = ['Jocab', 'Jonas', 'smith', 'lee', 'banner'] height = [160, 148, 231, 153, 162]Approach for Making a Parallel ArraySearching and sorting are some of the essential features required to form a parallel array.SearchingSearching is based on specific values ... Read More

Palindrome Substring Queries in C++

Prateek Jangid
Updated on 07-Mar-2022 07:59:03

553 Views

In this tutorial, we need to solve palindrome substring queries of the given string. Solving palindrome substring queries is far more complex than solving regular queries in C++. It requires a far more complex code and logic.In this tutorial, we are provided string str and Q number of substring[L...R] queries, each with two values L and R. we aim to write a program that will solve Queries to determine whether or not substring[L...R] is a palindrome. We must decide whether or not the substring formed within the range L to R is a palindrome to solve each query. For example ... Read More

Advertisements