Found 7197 Articles for C++

Maximize the product of four factors of a Number in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:24:30

244 Views

Given the task is to calculate the maximum product that can be obtained from four factors A, B, C, D of a given number N, given the condition −The sum of the four factors should be equal to the number N, that is, N=A+B+C+D.Input − N=10Output − 20Explanation − The factors of 10 are: 1, 2, 5, 10.Maximum product can be obtained by multiplying 5*2*2*1=20 and also it satisfies the given condition, that is, 5+2+2+1=10.Input − N=16Output − 256Explanation − The factors of 16 are: 1, 2, 4, 8, 16.Maximum product can be obtained by multiplying 4*4*4*4=256 and also it ... Read More

Maximize the given number by replacing a segment of digits with the alternate digits given in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:20:47

281 Views

Given the task is to maximize a given number with ‘N’ number of digits by replacing its digit using another array that contains 10 digits as an alternative for all single-digit numbers 0 to 9, The given condition is that only a consecutive segment of numbers can be replaced and only once.Input N=1234, arr[]={3 ,0 ,1 ,5 ,7 ,7 ,8 ,2 ,9 ,4}Output 1257Explanation The number 3 can be replaced with its alternative 5= arr[3]The number 4 can be replaced with its alternative 7= arr[4]Input N=5183, arr[]={3 ,0 ,1 ,5 ,7 ,7 ,8 ,2 ,9 ,4}Output 7183Approach used in the below program as followsIn function ... Read More

Maximize the profit by selling at-most M products in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:18:37

477 Views

Given the task is to calculate the maximum profit that can be made by selling at-most ‘M’ products.The total number of products are ‘N’ and the cost price and the selling price of each product is given in the lists CP[] and SP[] respectively.Input N=6, M=4 CP[]={1, 9, 5, 8, 2, 11} SP[]={1, 15, 10, 16, 5, 20}Output 28Explanation − The profit obtained from selling all the products are 0, 6, 5, 8, 3, 9 respectively.So, in order to make maximum profit by selling only 4 products, the products with the highest profit need to be chosen, that is, product number 2, ... Read More

Maximum difference elements that can added to a set in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:16:58

137 Views

According to the problem we are given a set arr[n] where n is the number of integer elements in the set, the task is to find the maximum difference elements which are to be added to obtain the elements in the set. In other words, the difference should be in form of |a-b| where 'a' and 'b' both are in the set and their difference should not be the least. So, we will count the maximum number of differences which are distinct and largest from a set. Let's understand the problem and its solution with help of an example.Input − ... Read More

Count number of Distinct Substring in a String in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:15:26

724 Views

According to the problem we are given a string str, we must count all the substrings in the given string. Substring is a string which is a part of an already existing string whose size may be smaller than or equal to the existing string.Let's understand the problem and its solution with the help of examples.Input − str = "wxyz";Output − count of distinct substring is: 10Explanation − Distinct substrings counted are −wxyz, wxy, wx, w, xyz, xy, x, yz, y, z so their count is 10Input − str = "zzzz"Output − count of distinct substring is: 4Explanation − Distinct ... Read More

Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++

Sunidhi Bansal
Updated on 13-Aug-2020 08:42:05

222 Views

We are given a positive integer N. The goal is to count the pairs of distinct non-negative positive integers that satisfy the inequality − x*x + y*y < N.We will start from x=0 to x2 lt; N and y=0 to y2 < N . If any x2 + y2 < N, increase count of pairs.Let us understand with examples −Input − n=4Output − distinct pairs= 4Explanation − pairs will be (0, 0), (1, 1), (0, 1), (1, 0). All these satisfy the inequality x2 + y2 < 4Input −n=2Output − distinct pairs= 3Explanation &minus pairs will be (0, 0), (0, ... Read More

Reasons for a C++ program crash

Sunidhi Bansal
Updated on 13-Aug-2020 08:37:52

1K+ Views

The abnormal behavior of C++ programs often leads to program crash. You may have encountered problems like Segmentation fault, Aborted, Floating point exception etc. Following are sample programs that may help you to understand the reasons for a C++ program crash.ExceptionsExceptions in C++ are responses of a program when it encounters an abnormal condition. The program crashes due to such exceptions if they are not handled properly using try-catch blocks. Following program crashes due to divide by zero exception −Example Live Demo#include int main(){    int num1=10;    int num2=0;    int quotient=num1/num2;    printf(" Quotient is: %d", quotient);   ... Read More

Product of all prime numbers in an Array in C++

Sunidhi Bansal
Updated on 13-Aug-2020 08:27:34

281 Views

Given an integer array arr[] with some elements, the task is to find the product of all the prime number of that numbers.Prime number are those number which are either divided by 1 or the number itself, or a prime number is a number which is not divisible by any other number except 1 and the number itself. Like 1, 2, 3, 5, 7, 11, etc.we have to find the solution for the given array −Input −arr[] = { 11, 20, 31, 4, 5, 6, 70 }Output − 1705Explanation − Prime numbers in array are − 11, 31, 5 their ... Read More

Product of factors of number in C++

Sunidhi Bansal
Updated on 13-Aug-2020 08:02:58

444 Views

Given a number n we have to find its all factors and find the product of those factors and return the result, i.e, the product of factors of a number. Factors of a number are those numbers which can divide the number completely including 1. Like factors of 6 are − 1, 2, 3, 6.Now according to the task we have to find the product all the factors of the number.Input − n = 18Output − 5832Explanation − 1 * 2 * 3 * 6 * 9 * 18 = 5832Input − n = 9Output − 27Explanation − 1 * 3 * 9 = 27Approach used below is as follows to solve the problem −Take the input num .Loop from i = 1 till i*i

Product of first N factorials in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:59:24

377 Views

Given a number N, the task is to find the product of first N factorials modulo by 1000000007. . Factorial implies when we find the product of all the numbers below that number including that number and is denoted by ! (Exclamation sign), For example − 4! = 4x3x2x1 = 24.So, we have to find a product of n factorials and modulo by 1000000007..Constraint 1 ≤ N ≤ 1e6.Input n = 9Output 27Explanation 1! * 2! * 3! * 4! * 5! * 6! * 7! * 8! * 9! Mod (1e9 + 7) = 27Input n = 3Output 12Explanation 1! * 2! * 3! mod (1e9 ... Read More

Advertisements