C# – Basic Exam – Biggest Triple
As I have mentioned here, I have participated in a competition for C# programmers. The second problem was concerning something quite standard – finding the biggest triple from a line of numbers. With other words, one get as an input the following numbers:
| 2 3 4 3 3 3 |
The program runs and gives as input the three sequential numbers, which have the highest sum. In our case this is 2 3 4 because it is the leftmost sequence as well (part of the description of the problem).
So, I have started with the problem quite well – I managed to note that we do not have a number, telling us how many numbers should we have in a column, thus I have decided to record all of the values as string. After this I simply recorded the values of the string into an array, splited by the space and the rest was quite easy – assigning the highest sum comparing it with the sum of the next digits.
Thus, within about 40 minutes I was able to have 100 points from this problem. However, I liked the fact that there were tests with negative numbers and thus I have failed some of the tests initially:
Anyway, at this task my code looked more presentable and understandable. Here it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main()
{
string myInput = Console.ReadLine();
string[] myArray = myInput.Split(' ');
int myArrayLength = myArray.Count();
int sum = -3333;
int newSum = -3333;
int a = 0;
int b = 0;
int c = 0;
int x = 0;
int y = 0;
int z = 0;
if (myArrayLength % 3 == 0)
{
for (int i = 0; i < myArrayLength; i++)
{
if ((i + 1) % 3 == 0)
{
x = int.Parse(myArray[i]);
y = int.Parse(myArray[i - 1]);
z = int.Parse(myArray[i - 2]);
newSum = x + y + z;
if (newSum > sum)
{
sum = newSum;
a = z;
b = y;
c = x;
}
}
}
Console.WriteLine("{0} {1} {2}", a, b, c);
}
else if (myArrayLength == 1)
{
Console.WriteLine("{0}", int.Parse(myArray[0]));
}
else if (myArrayLength % 3 == 1)
{
for (int i = 0; i < myArrayLength-1; i++)
{
if ((i + 1) % 3 == 0)
{
x = int.Parse(myArray[i]);
y = int.Parse(myArray[i - 1]);
z = int.Parse(myArray[i - 2]);
newSum = x + y + z;
if (newSum > sum)
{
sum = newSum;
a = z;
b = y;
c = x;
}
}
}
if (int.Parse((myArray[myArrayLength-1]))>sum)
{
Console.WriteLine(myArray[myArrayLength-1]);
}
else
{
Console.WriteLine("{0} {1} {2}", a, b, c);
}
}
else if (myArrayLength % 3 == 2)
{
for (int i = 0; i < myArrayLength - 2; i++)
{
if ((i + 1) % 3 == 0)
{
x = int.Parse(myArray[i]);
y = int.Parse(myArray[i - 1]);
z = int.Parse(myArray[i - 2]);
newSum = x + y + z;
if (newSum > sum)
{
sum = newSum;
a = z;
b = y;
c = x;
}
}
}
if ((int.Parse((myArray[myArrayLength - 1])) + int.Parse((myArray[myArrayLength - 2])) )> sum)
{
Console.WriteLine("{0} {1}", myArray[myArrayLength - 2], myArray[myArrayLength - 1]);
}
else
{
Console.WriteLine("{0} {1} {2}", a, b, c);
}
}
}
}
And the original solution from the author is present here:
using System;
class BiggestTriple
{
static void Main()
{
string inputLine = Console.ReadLine();
string[] numbers = inputLine.Split(' ');
int index = 0;
int maxSum = Int32.MinValue;
int start = 0;
while (index < numbers.Length)
{
int num1 = int.Parse(numbers[index]);
int num2 = 0;
if (index + 1 < numbers.Length)
{
num2 = int.Parse(numbers[index + 1]);
}
int num3 = 0;
if (index + 2 < numbers.Length)
{
num3 = int.Parse(numbers[index + 2]);
}
int sum = num1 + num2 + num3;
if (sum > maxSum)
{
maxSum = sum;
start = index;
}
index = index + 3;
}
// Print the result
while (maxSum != 0)
{
Console.Write(numbers[start]);
maxSum = maxSum - int.Parse(numbers[start]);
start++;
if (maxSum != 0)
{
Console.Write(" ");
}
}
}
}
Well so far so good 🙂
