Haskell Tape Equilibrium
tapeEquilibrium :: [Int] -> Int
tapeEquilibrium a = result
where
(_, _, result) = foldl step (0, sum a, maxBound) [0 .. length a - 2]
step (firstPart, secondPart, best) i =
let firstPart' = firstPart + i
secondPart' = secondPart - i
diff = abs (firstPart' - secondPart')
in (firstPart', secondPart', min best diff)
This keeps left and right running sums and updates the smallest difference at each split point.
Java Tape Equilibrium
public class Solution {
public static int tapeEquilibrium(int[] a) {
long firstPart = 0;
long secondPart = 0;
for (int v : a) {
secondPart += v;
}
long min = Long.MAX_VALUE;
for (int i = 0; i < a.length - 1; i++) {
firstPart += i;
secondPart -= i;
long difference = Math.abs(firstPart - secondPart);
min = Math.min(min, difference);
}
return (int) min;
}
}
This keeps left and right running sums and updates the smallest difference at each split point.
Lisp Tape Equilibrium
(defun tape-equilibrium (a)
(let* ((vec (coerce a 'vector))
(n (length vec))
(first-part 0)
(second-part (reduce #'+ vec))
(min most-positive-fixnum))
(loop for i from 0 below (1- n)
do (progn
(incf first-part i)
(decf second-part i)
(let ((difference (abs (- first-part second-part))))
(setf min (if (< difference min) difference min)))))
min))
This keeps left and right running sums and updates the smallest difference at each split point.
PHP Tape Equilibrium
function tapeEquilibrium(array $a): int
{
$firstPart = 0;
$secondPart = array_sum($a);
$min = PHP_INT_MAX;
for ($i = 0; $i < count($a) - 1; $i++) {
$firstPart += $i;
$secondPart -= $i;
$difference = 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.
Python Tape Equilibrium
def tape_equilibrium(a: list[int]) -> int:
first_part = 0
second_part = sum(a)
min_diff = float("inf")
for i in range(len(a) - 1):
first_part += i
second_part -= i
diff = abs(first_part - second_part)
min_diff = min(min_diff, diff)
return int(min_diff)
This keeps left and right running sums and updates the smallest difference at each split point.
Rust Tape Equilibrium
fn tape_equilibrium(a: &[i64]) -> i64 {
let mut first_part = 0i64;
let mut second_part: i64 = a.iter().sum();
let mut min = i64::MAX;
for i in 0..a.len() - 1 {
first_part += i as i64;
second_part -= i as i64;
let difference = (first_part - second_part).abs();
min = min.min(difference);
}
min
}
This keeps left and right running sums and updates the smallest difference at each split point.
TypeScript Tape Equilibrium
function tapeEquilibrium(a: number[]): number {
let firstPart = 0;
let secondPart = a.reduce((sum, v) => sum + v, 0);
let min = Infinity;
for (let i = 0; i < a.length - 1; i++) {
firstPart += i;
secondPart -= i;
const 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.
Bash Triangle
triangle() {
local -n _a="$1"
local -a _sorted=($(printf '%s\n' "${_a[@]}" | sort -n))
local _c=${#_sorted[@]}
if (( _c < 3 )); then
echo 0
return
fi
local _i
for ((_i = 0; _i < _c - 2; _i++)); do
if (( _sorted[_i] > 0 && _sorted[_i] > _sorted[_i+2] - _sorted[_i+1] )); then
echo 1
return
fi
done
echo 0
}
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
C++ Triangle
#include <algorithm>
#include <vector>
int triangle(std::vector<int> a)
{
std::sort(a.begin(), a.end());
int c = static_cast<int>(a.size());
if (c < 3) {
return 0;
}
for (int i = 0; i < c - 2; ++i) {
if (a[i] > 0 && a[i] > (a[i + 2] - a[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.
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.