Found 7347 Articles for C++

C++ Program to find out how many movies an attendee can watch entirely at a Film festival

Arnab Chakraborty
Updated on 20-Oct-2021 08:14:19

346 Views

Suppose there is a film festival going on that showcase various movies from various countries. Now, an attendee wants to attend the maximum number of movies that do not overlap with each other and we have to help them to find out how many movies they can attend.There is a structure Movie that has the following members −The beginning time of the movie.The duration of the movie.The ending time of the movie.There is another structure Festival with the following members −The number of movies at the festival.An array of type Movie whose size is similar to the number of movies ... Read More

Program to find out the k-th smallest difference between all element pairs in an array in C++

Arnab Chakraborty
Updated on 19-Oct-2021 12:19:45

293 Views

Suppose we are given a list containing several integer numbers. We have to find out the difference between each pair of values in the array and find out the k-th smallest difference number. The index starts at 0 and the value k is given to us as input.So, if the input is like numbers = {2, 6, 4, 8}, k = 2, then the output will be 2.The differences between the pairs are −(2, 6) = 4(2, 4) = 2(2, 8) = 6(6, 4) = 2(6, 8) = 2(4, 8) = 4If we sort the values, it becomes 2, 2, ... Read More

Program to express a positive integer number in words in C++

Arnab Chakraborty
Updated on 19-Oct-2021 12:54:22

232 Views

Suppose we are given a positive integer number. We have to spell the number in words; like if a number "56" is given as input the output will be "Fifty-Six". The range of conversion is up to a billion.So, if the input is like input = 5678, then the output will be Five Thousand Six Hundred Seventy-Eight.To solve this, we will follow these steps −Define an array ‘numbers’ that contain pairs such as − {{"Billion", 1000000000}, {"Million", 1000000}, {"Thousand", 1000}, {"Hundred", 100}, {"Ninety", 90}, {"Eighty", 80}, {"Seventy", 70}, {"Sixty", 60}, {"Fifty", 50}, {"Forty", 40}, {"Thirty", 30}, {"Twenty", 20}, {"Nineteen", 19}, ... Read More

Program to decode a given message in C++

Arnab Chakraborty
Updated on 19-Oct-2021 10:59:55

858 Views

Suppose we are given an encoded message that is a string of integer numbers. Now, these integer numbers can be mapped to a specific letter in the alphabet. a is mapped to 1, b is mapped to 2, c is mapped to 3, and so on. There is also a character '*' that can be in the message and that can be mapped to any of the numbers from 1 to 9. So given a message 'input', we have to find out how many ways it can be decoded.So, if the input is like input = "18", then the output ... Read More

Program to find out the cost to merge an array of integers into a single value in C++

Arnab Chakraborty
Updated on 19-Oct-2021 10:59:29

74 Views

Suppose we are given an array arr that contains n positive integer numbers. We are also given an integer number j. The task we have to perform is to merge j numbers into a single number by adding them. The cost of merging is equal to the addition of the j numbers we have selected. We have to find out the minimum possible cost for this merging operation.So, if the input is like arr = [2, 5, 6, 2, 3, 1, 3], j = 4, then the output will be 31.Cost to merge 2, 3, 1, 3 is equal to ... Read More

Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:08:35

217 Views

Suppose we are given a matrix that contains integer values. We have to find out the submatrices from the matrix where the sum of elements of the matrices is equal to a given target sum value. We return the number of submatrices.So, if the input is like0010010001011101and target = 5, then the output will be 3.The number of submatrices whose sum of elements is equal to 6 is 2.To solve this, we will follow these steps −n := size of matm := (if n is same as 0, then 0, otherwise size of mat[0])if m > n, then −Define one ... Read More

Program to find out the number of non-zero submatrices in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:02:14

368 Views

Suppose we are given a matrix that contains only two values; 1s and 0s. We have to find out the number of submatrices in the given matrix that contains all 1s. We print the value as output.So, if the input is like0010010001011101then the output will be 12.To solve this, we will follow these steps −n := size of matrixm := size of matrix[0]Define an array add of size: n+1 x m+1.for initialize i := 0, when i < n, update (increase i by 1), do −for initialize j := 0, when j < m, update (increase j by 1), do ... Read More

Program to find minimum cost to connect each Cartesian coordinates in C++

Arnab Chakraborty
Updated on 16-Oct-2021 11:53:51

180 Views

Suppose we have a list of 2D Cartesian coordinate points (x, y). We can connect (x0, y0) and (x1, y1), whose cost is |x0 - x1| + |y0 - y1|. If we are allowed to connect any number of points, we have to find the minimum cost necessary such that every point is connected by a path.So, if the input is like points = [[0, 0], [0, 2], [0, -2], [2, 0], [-2, 0], [2, 3], [2, -3]], then the output will be 14 because, from (0, 0) to (0, 2), (0, -2), (2, 0), (-2, 0), costs are 2, ... Read More

C++ Program to add few large numbers

Arnab Chakraborty
Updated on 12-Oct-2021 09:28:31

267 Views

Suppose we have an array nums of some large numbers. The large numbers are in range (-2^31 to 2^31 - 1). We have to find the sum of these numbers.So, if the input is like nums = [5000000003, 3000000005, 8000000007, 2000000009, 7000000011], then the output will be 25000000035.To solve this, we will follow these steps −x := 0for initialize i := 0, when i < size of nums, update (increase i by 1), do −x := x + nums[i]return xExampleLet us see the following implementation to get better understanding#include #include using namespace std; long long int solve(vector nums){    long long int x = 0;    for(int i=0; i

C++ program to demonstrate exception handling

Arnab Chakraborty
Updated on 12-Oct-2021 06:53:43

233 Views

Suppose there is a function that calculates some serious complex mathematical operations. But during the operations, some exceptions can occur. We have to handle the different types of exceptions that can occur and perform the following.If the computer is unable to allocate the memory for computation, we have to print 'Memory Low!'If any other C++-related exception occurs, we have to print 'Exception:' then the exception.If something else happens, we print 'Unhandled exception'.We are given an array that contains a pair of values, and we pass it to the function. If any exception occurs, we handle it, or otherwise, we print ... Read More

Advertisements