Maximize the value of the given expression
Last Updated :
07 Mar, 2022
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.
Examples:
Input: a = 2, b = 1, c = 4
Output: 12
(1 + 2) * 4 = 3 * 4 = 12
Input: a = 2, b = 2, c = 2
Output: 8
(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:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumResult( int a, int b, int c)
{
int countOfNegative = 0;
int sum = a + b + c;
int product = a * b * c;
int largest = max(a,max(b,c));
int smallest = min(a,min(b,c) );
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
switch (countOfNegative) {
case 0:
return (sum - largest) * largest;
case 1:
return (product / smallest) + smallest;
case 2:
return (product / largest) + largest;
case 3:
return (sum - smallest) * smallest;
}
}
int main()
{
int a=-2,b=-1,c=-4;
cout << maximumResult(a, b, c);
return 0;
}
|
Java
class GFG
{
static int maximumResult( int a, int b, int c)
{
int countOfNegative = 0 ;
int sum = a + b + c;
int product = a * b * c;
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest= (a<b)?((a<c)? a : c):((b<c) ? b : c);
if (a < 0 )
countOfNegative++;
if (b < 0 )
countOfNegative++;
if (c < 0 )
countOfNegative++;
switch (countOfNegative)
{
case 0 :
return (sum - largest) * largest;
case 1 :
return (product / smallest) + smallest;
case 2 :
return (product / largest) + largest;
case 3 :
return (sum - smallest) * smallest;
}
return - 1 ;
}
public static void main(String[] args)
{
int a=- 2 ,b=- 1 ,c=- 4 ;
System.out.print(maximumResult(a, b, c));
}
}
|
Python3
def maximumResult(a, b, c):
countOfNegative = 0
Sum = a + b + c
product = a * b * c
largest = max (a, b, c)
smallest = min (a, b, c)
if a < 0 :
countOfNegative + = 1
if b < 0 :
countOfNegative + = 1
if c < 0 :
countOfNegative + = 1
if countOfNegative = = 0 :
return ( Sum - largest) * largest
elif countOfNegative = = 1 :
return (product / / smallest) + smallest
elif countOfNegative = = 2 :
return (product / / largest) + largest
elif countOfNegative = = 3 :
return ( Sum - smallest) * smallest
if __name__ = = "__main__" :
a, b, c = - 2 , - 1 , - 4
print (maximumResult(a, b, c))
|
C#
using System;
class GFG
{
static int maximumResult( int a, int b, int c)
{
int countOfNegative = 0;
int sum = a + b + c;
int product = a * b * c;
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest=(a<b)?((a<c)? a : c):((b<c) ? b : c);
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
switch (countOfNegative)
{
case 0:
return (sum - largest) * largest;
case 1:
return (product / smallest) + smallest;
case 2:
return (product / largest) + largest;
case 3:
return (sum - smallest) * smallest;
}
return -1;
}
static void Main()
{
int a = -2, b = -1, c = -4;
Console.WriteLine(maximumResult(a, b, c));
}
}
|
PHP
<?php
function maximumResult( $a , $b , $c )
{
$countOfNegative = 0;
$sum = $a + $b + $c ;
$product = $a * $b * $c ;
$largest = max( $a , $b , $c );
$smallest = min( $a , $b , $c );
if ( $a < 0)
$countOfNegative ++;
if ( $b < 0)
$countOfNegative ++;
if ( $c < 0)
$countOfNegative ++;
switch ( $countOfNegative )
{
case 0:
return ( $sum - $largest ) *
$largest ;
case 1:
return ( $product / $smallest ) +
$smallest ;
case 2:
return ( $product / $largest ) +
$largest ;
case 3:
return ( $sum - $smallest ) *
$smallest ;
}
}
$a = -2;
$b = -1;
$c = -4;
echo maximumResult( $a , $b , $c );
?>
|
Javascript
<script>
function maximumResult(a, b, c)
{
let countOfNegative = 0;
let sum = a + b + c;
let product = a * b * c;
let largest = Math.max(a,Math.max(b,c));
let smallest = Math.min(a,Math.min(b,c) );
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
switch (countOfNegative) {
case 0:
return (sum - largest) * largest;
case 1:
return (product / smallest) + smallest;
case 2:
return (product / largest) + largest;
case 3:
return (sum - smallest) * smallest;
}
}
let a = -2, b = -1, c = -4;
document.write(maximumResult(a, b, c));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...