C++ Perm Check
#include <algorithm>
#include <cstddef>
#include <vector>
int permCheck(std::vector<int> a)
{
std::sort(a.begin(), a.end());
for (std::size_t k = 0; k < a.size(); ++k) {
if (k + 1 < a.size() && a[k] != static_cast<int>(k) + 1) {
return 0;
}
}
return 1;
}
This validates that every value from 1 to N appears exactly once.
C# Perm Check
static int PermCheck(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
for (int k = 0; k < sorted.Length; k++)
{
if (k + 1 < sorted.Length && sorted[k] != k + 1)
{
return 0;
}
}
return 1;
}
This validates that every value from 1 to N appears exactly once.
Elixir Perm Check
defmodule PermCheck do
def perm_check(a) do
sorted = Enum.sort(a)
size = length(sorted)
ok =
0..(size - 2)//1
|> Enum.all?(fn k -> Enum.at(sorted, k) == k + 1 end)
if ok, do: 1, else: 0
end
end
This validates that every value from 1 to N appears exactly once.
Erlang Perm Check
-module(perm_check).
-export([perm_check/1]).
perm_check(A) ->
Sorted = lists:sort(A),
Indexed = lists:zip(lists:seq(1, length(Sorted)), Sorted),
case lists:all(fun({I, V}) -> I =:= V end, Indexed) of
true -> 1;
false -> 0
end.
This validates that every value from 1 to N appears exactly once.
Go Perm Check
func permCheck(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
for k, v := range sorted {
if k+1 < len(sorted) && v != k+1 {
return 0
}
}
return 1
}
This validates that every value from 1 to N appears exactly once.
Haskell Perm Check
import Data.List (sort)
permCheck :: [Int] -> Int
permCheck a = if sort a == [1 .. length a] then 1 else 0
This validates that every value from 1 to N appears exactly once.
Java Perm Check
import java.util.Arrays;
public class Solution {
public static int permCheck(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
for (int k = 0; k < sorted.length - 1; k++) {
if (sorted[k] != k + 1) {
return 0;
}
}
return 1;
}
}
This validates that every value from 1 to N appears exactly once.
Lisp Perm Check
(defun perm-check (a)
(let* ((sorted (sort (copy-list a) #'<))
(vec (coerce sorted 'vector))
(n (length vec)))
(loop for k from 0 below n
do (when (and (< (1+ k) n) (/= (aref vec k) (1+ k)))
(return-from perm-check 0)))
1))
This validates that every value from 1 to N appears exactly once.
PHP Perm Check
function permCheck($a): int
{
sort($a);
foreach ($a as $k => $i) {
if (isset($a[$k + 1]) && $i !== $k + 1) {
return 0;
}
}
return 1;
}
This validates that every value from 1 to N appears exactly once.
Python Perm Check
def perm_check(a: list[int]) -> int:
a = sorted(a)
for k, v in enumerate(a):
if k + 1 < len(a) and v != k + 1:
return 0
return 1
This validates that every value from 1 to N appears exactly once.