Elixir Triangle
defmodule Triangle do
def triangle(a) when length(a) < 3, do: 0
def triangle(a) do
sorted = Enum.sort(a)
c = length(sorted)
found =
Enum.any?(0..(c - 3), fn i ->
v = Enum.at(sorted, i)
v > 0 and v > Enum.at(sorted, i + 2) - Enum.at(sorted, i + 1)
end)
if found, do: 1, else: 0
end
end
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
Erlang Triangle
-module(triangle).
-export([triangle/1]).
triangle(A) ->
Sorted = lists:sort(A),
N = length(Sorted),
case N < 3 of
true -> 0;
false ->
Triples = lists:zip3(Sorted, tl(Sorted), tl(tl(Sorted))),
case lists:any(fun({X, Y, Z}) -> X > 0 andalso X > (Z - Y) end, Triples) of
true -> 1;
false -> 0
end
end.
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
Go Triangle
func triangle(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
c := len(sorted)
if c < 3 {
return 0
}
for i := 0; i < c-2; i++ {
if sorted[i] > 0 && sorted[i] > 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.
Haskell Triangle
import Data.List (sort)
triangle :: [Int] -> Int
triangle a
| length s < 3 = 0
| any valid (zip3 s (drop 1 s) (drop 2 s)) = 1
| otherwise = 0
where
s = sort a
valid (x, y, z) = x > 0 && x > (z - y)
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
Java Triangle
import java.util.Arrays;
public class Solution {
public static int triangle(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
int 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.
Lisp Triangle
(defun triangle (a)
(let* ((sorted (sort (copy-list a) #'<))
(vec (coerce sorted 'vector))
(c (length vec)))
(if (< c 3)
0
(progn
(loop for i from 0 below (- c 2)
do (when (and (> (aref vec i) 0)
(> (aref vec i) (- (aref vec (+ i 2)) (aref vec (1+ i)))))
(return-from triangle 1)))
0))))
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
PHP Triangle
function triangle(array $a): int
{
sort($a);
$c = count($a);
if ($c < 3) {
return 0;
}
for ($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.
Python Triangle
def triangle(a: list[int]) -> int:
a = sorted(a)
c = len(a)
if c < 3:
return 0
for i in range(c - 2):
if a[i] > 0 and 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.
Rust Triangle
fn triangle(mut a: Vec<i64>) -> i64 {
a.sort();
let c = a.len();
if c < 3 {
return 0;
}
for i in 0..c - 2 {
if a[i] > 0 && a[i] > a[i + 2] - a[i + 1] {
return 1;
}
}
0
}
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.
TypeScript Triangle
function triangle(a: number[]): number {
const sorted = [...a].sort((x, y) => x - y);
const c = sorted.length;
if (c < 3) {
return 0;
}
for (let i = 0; i < c - 2; i++) {
if (sorted[i] > 0 && sorted[i] > 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.