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:
object Solution { def solution(a: Array[Int]): Int }
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 Scala
Time spent on task 6 minutes
Notes
not defined yet
Task timeline
Code: 04:58:49 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
import scala.collection.BitSet
@scala.annotation.tailrec
def go(index: Int, remaining: BitSet): Int = {
if (index >= A.length) {
if (remaining.isEmpty) 1 else 0
} else {
val currentValue = A(index)
if (!remaining.contains(currentValue)) 0
else go(index + 1, remaining - currentValue)
}
}
go(0, BitSet(1 to A.length: _*))
}
}
Analysis
Code: 04:59:10 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
import scala.collection.BitSet
@scala.annotation.tailrec
def go(index: Int, remaining: BitSet): Int = {
if (index >= A.length) {
if (remaining.isEmpty) 1 else 0
} else {
val currentValue = A(index)
if (!remaining.contains(currentValue)) 0
else go(index + 1, remaining - currentValue)
}
}
go(0, BitSet(1 to A.length: _*))
}
}
Analysis
Code: 04:59:26 UTC,
scala,
final,
score: 
100
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
import scala.collection.BitSet
@scala.annotation.tailrec
def go(index: Int, remaining: BitSet): Int = {
if (index >= A.length) {
if (remaining.isEmpty) 1 else 0
} else {
val currentValue = A(index)
if (!remaining.contains(currentValue)) 0
else go(index + 1, remaining - currentValue)
}
}
go(0, BitSet(1 to A.length: _*))
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
2.366 s
OK
2.
2.376 s
OK
1.
2.379 s
OK
2.
2.348 s
OK
1.
2.360 s
OK
2.
2.358 s
OK
3.
2.343 s
OK
4.
2.341 s
OK
1.
2.360 s
OK
2.
2.372 s
OK
3.
2.349 s
OK
4.
2.375 s
OK
1.
2.391 s
OK
2.
2.342 s
OK
expand all
Performance tests
1.
2.996 s
OK
2.
3.057 s
OK
1.
3.304 s
OK
2.
3.731 s
OK
1.
3.685 s
OK
2.
3.637 s
OK
1.
3.606 s
OK
2.
3.667 s
OK
1.
2.355 s
OK
2.
3.165 s
OK
3.
2.373 s
OK