Given three non-zero integers a, b and c. The task is to find the maximum value possible by putting addition and multiplication signs between them in any order.
Note: Rearrangement of integers is allowed but addition and multiplication sign must be used once. Braces can also be placed between equations as per your need.
Input: a = 2, b = 1, c = 4
(1 + 2) * 4 = 3 * 4 = 12
Input: a = 2, b = 2, c = 2
(2 + 2) * 2 = 4 * 2 = 8
Approach: To solve this problem one can opt the method of generating all the possibilities and calculate them to get the maximum value but this approach is not efficient. Take the advantage of given conditions that integers may got rearranged and mandatory use of each mathematical sign (+, *). There are total of four cases to solve which are listed below:
- All three integers are non-negative: For this simply add two smaller one and multiply their result by largest integer.
- One integer is negative and rest two positive : Multiply the both positive integer and add their result to negative integer.
- Two integers are negative and one is positive: As the product of two negative numbers is positive multiply both negative integers and then add their result to positive integer.
- All three are negative integers: add the two largest integers and multiply them to smallest one. case 3-: (sum – smallest) * smallest
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Find minimum value of the expression by choosing K elements from given array
- Find the minimum value of the given expression over all pairs of the array
- Maximize a value for a semicircle of given radius
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Find the minimum value of X for an expression
- Find Range Value of the Expression
- Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet
- Parity of the given mathematical expression using given N numbers
- Maximize the value of x + y + z such that ax + by + cz = n
- Maximize value of (a+b) such that (a*a-b*b = N)
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Maximize the Sum of the given array using given operations
- Find all possible outcomes of a given expression
- Find the missing digit x from the given expression
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Print the balanced bracket expression using given brackets
- Maximize volume of cuboid with given sum of sides
- Maximize the median of the given array after adding K elements to the same array
- Remove an element to maximize the GCD of the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.