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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum result
int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = max(a,max(b,c));
int smallest = min(a,min(b,c) );
// Calculate the count of negative integers
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
// Depending upon count of negatives
switch (countOfNegative) {
// When all three are positive integers
case 0:
return (sum - largest) * largest;
// For single negative integer
case 1:
return (product / smallest) + smallest;
// For two negative integers
case 2:
return (product / largest) + largest;
// For three negative integers
case 3:
return (sum - smallest) * smallest;
}
}
// Driver Code
int main()
{
int a=-2,b=-1,c=-4;
cout << maximumResult(a, b, c);
return 0;
}
// This code contributed by Nikhil
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum result
static int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest= (a<b)?((a<c)? a : c):((b<c) ? b : c);
// Calculate the count of negative integers
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
// Depending upon count of negatives
switch (countOfNegative)
{
// When all three are positive integers
case 0:
return (sum - largest) * largest;
// For single negative integer
case 1:
return (product / smallest) + smallest;
// For two negative integers
case 2:
return (product / largest) + largest;
// For three negative integers
case 3:
return (sum - smallest) * smallest;
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int a=-2,b=-1,c=-4;
System.out.print(maximumResult(a, b, c));
}
}
// This code contributed by Nikhil
Python3
# Python3 implementation of the approach
# Function to return the maximum result
# Python3 implementation of the approach
# Function to return the maximum result
def maximumResult(a, b, c):
# To store the count of negative integers
countOfNegative = 0
# Sum of all the three integers
Sum = a + b + c
# Product of all the three integers
product = a * b * c
# To store the smallest and the
# largest among all the three integers
largest = max(a, b, c)
smallest = min(a, b, c)
# Calculate the count of negative integers
if a < 0:
countOfNegative += 1
if b < 0:
countOfNegative += 1
if c < 0:
countOfNegative += 1
# When all three are positive integers
if countOfNegative == 0:
return (Sum - largest) * largest
# For single negative integer
elif countOfNegative == 1:
return (product // smallest) + smallest
# For two negative integers
elif countOfNegative == 2:
return (product // largest) + largest
# For three negative integers
elif countOfNegative == 3:
return (Sum - smallest) * smallest
# Driver Code
if __name__ == "__main__":
a, b, c = -2, -1, -4
print(maximumResult(a, b, c))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum result
static int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest=(a<b)?((a<c)? a : c):((b<c) ? b : c);
// Calculate the count of negative integers
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
// Depending upon count of negatives
switch (countOfNegative)
{
// When all three are positive integers
case 0:
return (sum - largest) * largest;
// For single negative integer
case 1:
return (product / smallest) + smallest;
// For two negative integers
case 2:
return (product / largest) + largest;
// For three negative integers
case 3:
return (sum - smallest) * smallest;
}
return -1;
}
// Driver Code
static void Main()
{
int a = -2, b = -1, c = -4;
Console.WriteLine(maximumResult(a, b, c));
}
}
// This code is contributed by mits & Nikhil
PHP
<?php
// PHP implementation of the approach
// Function to return the maximum result
function maximumResult($a, $b, $c)
{
// To store the count of
// negative integers
$countOfNegative = 0;
// Sum of all the three integers
$sum = $a + $b + $c;
// Product of all the three integers
$product = $a * $b * $c;
// To store the smallest and the largest
// among all the three integers
$largest = max($a ,$b, $c);
$smallest = min($a ,$b, $c);
// Calculate the count of negative integers
if ($a < 0)
$countOfNegative++;
if ($b < 0)
$countOfNegative++;
if ($c < 0)
$countOfNegative++;
// Depending upon count of negatives
switch ($countOfNegative)
{
// When all three are positive integers
case 0:
return ($sum - $largest) *
$largest;
// For single negative integer
case 1:
return ($product / $smallest) +
$smallest;
// For two negative integers
case 2:
return ($product / $largest) +
$largest;
// For three negative integers
case 3:
return ($sum - $smallest) *
$smallest;
}
}
// Driver Code
$a = -2;
$b = -1;
$c = -4;
echo maximumResult($a, $b, $c);
// This code is contributed by ihritik
?>
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the maximum result
function maximumResult(a, b, c)
{
// To store the count of negative integers
let countOfNegative = 0;
// Sum of all the three integers
let sum = a + b + c;
// Product of all the three integers
let product = a * b * c;
// To store the smallest and the largest
// among all the three integers
let largest = Math.max(a,Math.max(b,c));
let smallest = Math.min(a,Math.min(b,c) );
// Calculate the count of negative integers
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
// Depending upon count of negatives
switch (countOfNegative) {
// When all three are positive integers
case 0:
return (sum - largest) * largest;
// For single negative integer
case 1:
return (product / smallest) + smallest;
// For two negative integers
case 2:
return (product / largest) + largest;
// For three negative integers
case 3:
return (sum - smallest) * smallest;
}
}
// Driver Code
let a = -2, b = -1, c = -4;
document.write(maximumResult(a, b, c));
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find X such that the given expression is maximized Given two integers A and B, the task is to output the maximum value of (X % B) * ((A â X) % B) by finding the appropriate value of X such that (0 <= X <= A). Note: If there are multiple values of X, output any valid value. Examples: Input: A = 4, B = 7Output: 2Explanation: As we know choose th
5 min read
Maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array Given an array A[] of length N and an integer K, the task is to maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array, where (1 ⤠i < j ⤠N) and | denotes Bitwise OR operator. Examples: Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10Output: 2Explanation: The maximum
6 min read
Maximize value of given expression by choosing pair from Array Given an array A[] of length N, choose any two elements x and y from the array, the task is to find the maximum value of the expression (x * y + x - y). Examples: Input: A[] = {5, 2, 3} Output: 17Explanation: There are six possible pairs:For pairs {2, 3} and {3, 2}, answer = 2 ? 3 + max(2?3, 3?2) =
5 min read
Best order to maximise the value Given, an array arr[] of N integers, the task is to select some integers from this array arr[] and arrange them in another array brr[], such that the sum of brr[i]*(i+1) for every index in array brr is maximum. Find out the maximum possible such value for a given array arr. Input: N = 5, arr[] = {-1
6 min read
Find out the maximum value of the expression according to the given constraint Given an array A[] of length N. Then your task is to output the maximum value of (Sum/LCM). Where Sum is the addition of all the elements of A[] and LCM is a number between [1, â] that can't be achieved by any possible non-empty set of A[]. Examples: Input: N = 5, A[] = {1, 2, 3, 4, 5} Output: 2 Exp
9 min read