C# Tape Equilibrium
static long TapeEquilibrium(int[] a)
{
long firstPart = 0;
long secondPart = a.Sum(x => (long)x);
long min = long.MaxValue;
for (int i = 0; i < a.Length - 1; i++)
{
firstPart += i;
secondPart -= i;
var difference = Math.Abs(firstPart - secondPart);
min = difference < min ? difference : min;
}
return min;
}
This keeps left and right running sums and updates the smallest difference at each split point.
C# Triangle
static int Triangle(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
var c = sorted.Length;
if (c < 3)
{
return 0;
}
for (int i = 0; i < c - 2; i++)
{
if (sorted[i] > 0 && sorted[i] > (long)sorted[i + 2] - sorted[i + 1])
{
return 1;
}
}
return 0;
}
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.