Created
December 1, 2015 06:09
-
-
Save divmgl/d7ec6299f25d822617d1 to your computer and use it in GitHub Desktop.
PermCheck Javascript 100%/100%
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(A) { | |
var L = A.length; // Length of the array | |
var X = ((L + 1) * L) / 2; // Sum from 1..L | |
var Y = 0; // Sum of evaluated values | |
var I = 0; // Counter | |
var F = []; // Found elements | |
var V = -1; // Container | |
while (I < L) { // Start searching the array | |
V = A[I]; // Get the value | |
I++; // Increase the counter | |
if (F[V]) continue; // If we've accounted for this, continue | |
F[V] = true; // Record the value | |
Y += V; // Add to the evaluated sum | |
} | |
// If the evaluated sum is equal to the sum of the array length | |
// then we can be sure that the array is a permutation. | |
return X === Y ? 1 : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment