Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
100%
Correctness
100%
Performance
100%
This is a demo task.
Write a function:
object Solution { def solution(a: Array[Int]): Int }
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
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,000,000..1,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 7 minutes
Notes
not defined yet
Task timeline
Code: 02:28:51 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 = {
// First build set of integers that exist.
@scala.annotation.tailrec
def buildSet(index: Int, accum: Set[Int]): Set[Int] = {
if (index >= A.length) accum
else {
val nextValue = A(index)
val nextAccum =
if (nextValue > 0) accum + nextValue
else accum
buildSet(index + 1, nextAccum)
}
}
val positiveValues = buildSet(0, Set.empty)
// Next find lowest number not in set.
@scala.annotation.tailrec
def findLowest(i: Int): Int = {
if (positiveValues contains i) findLowest(i + 1)
else i
}
findLowest(1)
}
}
Analysis
Code: 02:29:30 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 = {
// First build set of integers that exist.
@scala.annotation.tailrec
def buildSet(index: Int, accum: Set[Int]): Set[Int] = {
if (index >= A.length) accum
else {
val nextValue = A(index)
val nextAccum =
if (nextValue > 0) accum + nextValue
else accum
buildSet(index + 1, nextAccum)
}
}
val positiveValues = buildSet(0, Set.empty)
// Next find lowest number not in set.
@scala.annotation.tailrec
def findLowest(i: Int): Int = {
if (positiveValues contains i) findLowest(i + 1)
else i
}
findLowest(1)
}
}
Analysis
Code: 02:29:44 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 = {
// First build set of integers that exist.
@scala.annotation.tailrec
def buildSet(index: Int, accum: Set[Int]): Set[Int] = {
if (index >= A.length) accum
else {
val nextValue = A(index)
val nextAccum =
if (nextValue > 0) accum + nextValue
else accum
buildSet(index + 1, nextAccum)
}
}
val positiveValues = buildSet(0, Set.empty)
// Next find lowest number not in set.
@scala.annotation.tailrec
def findLowest(i: Int): Int = {
if (positiveValues contains i) findLowest(i + 1)
else i
}
findLowest(1)
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
2.041 s
OK
2.
2.305 s
OK
3.
2.296 s
OK
1.
2.319 s
OK
2.
2.448 s
OK
1.
2.304 s
OK
2.
2.442 s
OK
1.
2.452 s
OK
1.
2.309 s
OK
expand all
Performance tests
1.
2.824 s
OK
2.
2.809 s
OK
3.
2.990 s
OK
1.
3.350 s
OK
1.
3.231 s
OK
2.
3.375 s
OK
1.
3.057 s
OK