-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmaximizethevalue.cpp
61 lines (47 loc) · 1.16 KB
/
maximizethevalue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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