Tasks Details
easy
1.
PermCheck
Check whether array A is a permutation.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given.
A permutation is a sequence containing each element from 1 to N once, and only once.
For example, array A such that:
A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2is a permutation, but array A such that:
A[0] = 4 A[1] = 1 A[2] = 3is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
function solution(A);
that, given an array A, returns 1 if array A is a permutation and 0 if it is not.
For example, given array A such that:
A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2the function should return 1.
Given array A such that:
A[0] = 4 A[1] = 1 A[2] = 3the function should return 0.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [1..1,000,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 12 minutes
Notes
not defined yet
Code: 09:55:51 UTC,
java,
autosave
Code: 09:56:42 UTC,
js,
autosave
Code: 09:56:52 UTC,
js,
autosave
Code: 09:57:20 UTC,
js,
autosave
Code: 09:57:31 UTC,
js,
autosave
Code: 09:58:01 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== )
}
}
Code: 09:58:18 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
}
}
}
Code: 09:58:30 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else
}
}
Code: 09:58:40 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
}
}
}
Code: 09:58:59 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = i
}
}
}
Code: 09:59:22 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true
}
}
}
Code: 09:59:52 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true
}
}
for (let i = 0; i < result)
}
Code: 10:00:07 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true
}
}
for (let i = 0; i < resultArray.length; i++) {
}
}
Code: 10:00:36 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:00:43 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Analysis
Code: 10:01:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:01:27 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:01:29 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
User test case 1:
[1, 1, 2]
User test case 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 3:
[1, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 4:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 1
function result: 1
Code: 10:02:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:02:52 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
User test case 1:
[1, 1, 2]
User test case 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 3:
[1, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 4:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.072 s
OK
function result: 1
function result: 1
Code: 10:03:03 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:03:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:04:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:04:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:06:25 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:06:55 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Code: 10:07:02 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
User test case 1:
[1, 1, 2]
User test case 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 3:
[1, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 4:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 1
function result: 1
Code: 10:07:18 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
User test case 1:
[1, 1, 2]
User test case 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 3:
[1, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 4:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
User test case 5:
[100, 1, 2, 3, 4]
Analysis
Code: 10:07:29 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
User test case 1:
[1, 1, 2]
User test case 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 3:
[1, 3, 4, 5, 6, 7, 8, 9, 10]
User test case 4:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
User test case 5:
[100, 1, 2, 3, 4]
Analysis
Code: 10:07:32 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let resultArray = [0];
for (let i = 0; i < A.length; i++) {
if(A[i] > A.length || typeof resultArray[A[i]] !== 'undefined') {
return 0;
} else {
resultArray[A[i]] = true;
}
}
for (let i = 0; i < resultArray.length; i++) {
if (typeof resultArray[i] == "undefined") {
return 0;
}
}
return 1;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
permutations_of_ranges
permutations of sets like [2..100] for which the anwsers should be false
permutations of sets like [2..100] for which the anwsers should be false
✔
OK
1.
0.076 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
expand all
Performance tests
1.
0.080 s
OK
2.
0.080 s
OK
1.
0.104 s
OK
2.
0.112 s
OK
1.
0.120 s
OK
2.
0.120 s
OK
1.
0.112 s
OK
2.
0.112 s
OK
1.
0.068 s
OK
2.
0.096 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.084 s
OK
4.
0.116 s
OK
5.
0.120 s
OK