Last active
February 20, 2019 14:48
-
-
Save WennderSantos/e25df9ee64d267228914b4120ece63d1 to your computer and use it in GitHub Desktop.
Find max product of three numbers in an array of integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var input = new int[] { -60, -3, 5, 4, 8, 1, 10, 15, 17 }; | |
(int first, int second, int third) maxValues = (int.MinValue, int.MinValue, int.MinValue); | |
(int first, int second) minValues = (int.MaxValue, int.MaxValue); | |
((int first, int second, int third), (int first, int second)) values = (maxValues, minValues); | |
for (var i = 0; i < input.Length; i++) | |
values = Rearenge(values.Item1, values.Item2, input[i]); | |
Console.WriteLine(Math.Max(values.Item1.first * values.Item1.second * values.Item1.third, values.Item1.first * values.Item2.first * values.Item2.second)); | |
} | |
static ((int, int, int), (int, int)) Rearenge((int first, int second, int third) maxValues, (int first, int second) minValues, int input) | |
{ | |
if (maxValues.first < input) | |
{ | |
maxValues.third = maxValues.second; | |
maxValues.second = maxValues.first; | |
maxValues.first = input; | |
} | |
else if (maxValues.second < input && maxValues.first != input) | |
{ | |
maxValues.third = maxValues.second; | |
maxValues.second = input; | |
} | |
else if (maxValues.third < input && maxValues.second != input) | |
maxValues.third = input; | |
if (minValues.first > input) | |
{ | |
minValues.second = minValues.first; | |
minValues.first = input; | |
} | |
else if (minValues.second > input) | |
minValues.second = input; | |
return (maxValues, minValues); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment