Tasks Details
medium
Find the smallest positive integer that does not occur in a given sequence.
Task Score
77%
Correctness
100%
Performance
50%
This is a demo task.
Write a function:
def solution(a)
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 Ruby
Time spent on task 9 minutes
Notes
not defined yet
Code: 14:50:18 UTC,
java,
autosave
Code: 14:50:35 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select(&:positive?)
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5:in `select': undefined method `positive?' for 1:Fixnum (NoMethodError) from exec.rb:5:in `solution' from exec.rb:118:in `<main>'
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5:in `select': undefined method `positive?' for 1:Fixnum (NoMethodError) from exec.rb:5:in `solution' from exec.rb:118:in `<main>'
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5:in `select': undefined method `positive?' for -3:Fixnum (NoMethodError) from exec.rb:5:in `solution' from exec.rb:118:in `<main>'
Code: 14:51:15 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select(&:?)
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Code: 14:51:31 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select(|i| i > 0 )
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5: syntax error, unexpected '|', expecting ')' positive_sorted = a.sort.select(|i| i > 0 ) ^ exec.rb:5: syntax error, unexpected ')', expecting keyword_end
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5: syntax error, unexpected '|', expecting ')' positive_sorted = a.sort.select(|i| i > 0 ) ^ exec.rb:5: syntax error, unexpected ')', expecting keyword_end
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:5: syntax error, unexpected '|', expecting ')' positive_sorted = a.sort.select(|i| i > 0 ) ^ exec.rb:5: syntax error, unexpected ')', expecting keyword_end
Code: 14:51:45 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Code: 14:51:46 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:10:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:10:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:118:in `<main>'
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:10:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:10:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:118:in `<main>'
1.
0.044 s
OK
Code: 14:52:49 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Code: 14:52:54 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.sort.select {|i| i > 0 }
puts positive_sorted
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:11:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:11:in `block in solution' from exec.rb:9:in `each' from exec.rb:9:in `solution' from exec.rb:119:in `<main>'stdout:
1 1 2 3 4 6
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:11:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:11:in `block in solution' from exec.rb:9:in `each' from exec.rb:9:in `solution' from exec.rb:119:in `<main>'stdout:
1 2 3
1.
0.044 s
OK
Code: 14:53:08 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
puts positive_sorted
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:11:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:11:in `block in solution' from exec.rb:9:in `each' from exec.rb:9:in `solution' from exec.rb:119:in `<main>'stdout:
1 2 3 4 6
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:11:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:11:in `block in solution' from exec.rb:9:in `each' from exec.rb:9:in `solution' from exec.rb:119:in `<main>'stdout:
1 2 3
1.
0.040 s
OK
Code: 14:53:41 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
puts positive_sorted
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.any?(res)
return res
end
end
Code: 14:53:59 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
puts positive_sorted
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
next if positive_sorted.any?(res)
return res
end
end
Code: 14:54:22 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
puts "pos_s: #{positive_sorted}"
next if positive_sorted.any?(res)
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:12:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:12:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:120:in `<main>'stdout:
res: 2 pos_s: [1, 2, 3, 4, 6]
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:12:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:12:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:120:in `<main>'stdout:
res: 2 pos_s: [1, 2, 3]
1.
0.044 s
OK
Code: 14:55:05 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
puts "pos_s: #{positive_sorted}"
next if positive_sorted.any?es)
return res
end
end
Code: 14:55:11 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
puts "pos_s: #{positive_sorted}"
next if positive_sorted.any? res
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:12:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:12:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:120:in `<main>'stdout:
res: 2 pos_s: [1, 2, 3, 4, 6]
1.
0.044 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
exec.rb:12:in `any?': wrong number of arguments (1 for 0) (ArgumentError) from exec.rb:12:in `block in solution' from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:120:in `<main>'stdout:
res: 2 pos_s: [1, 2, 3]
1.
0.044 s
OK
Code: 14:55:45 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
puts "pos_s: #{positive_sorted}"
next if positive_sorted.? res
return res
end
end
Code: 14:55:48 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
puts "res: #{res}"
puts "pos_s: #{positive_sorted}"
next if positive_sorted.include? res
return res
end
end
Analysis
expand all
Example tests
1.
0.044 s
OK
stdout:
res: 2 pos_s: [1, 2, 3, 4, 6] res: 3 pos_s: [1, 2, 3, 4, 6] res: 4 pos_s: [1, 2, 3, 4, 6] res: 5 pos_s: [1, 2, 3, 4, 6]
1.
0.044 s
OK
stdout:
res: 2 pos_s: [1, 2, 3] res: 3 pos_s: [1, 2, 3] res: 4 pos_s: [1, 2, 3]
1.
0.044 s
OK
Code: 14:57:54 UTC,
rb,
autosave
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.include? res
return res
end
end
Code: 14:58:03 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.include?(res)
return res
end
end
Analysis
Code: 14:58:22 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.include?(res)
return res
end
end
Analysis
Code: 14:58:24 UTC,
rb,
final,
score: 
77
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
positive_sorted = a.uniq.sort.select {|i| i > 0 }
return 1 if (positive_sorted.empty? || positive_sorted.first > 1)
positive_sorted.each do |i|
res = i + 1
next if positive_sorted.include?(res)
return res
end
end
Analysis summary
The following issues have been detected: timeout errors.
Analysis
Detected time complexity:
O(N**2)
expand all
Correctness tests
1.
0.044 s
OK
2.
0.044 s
OK
3.
0.044 s
OK
4.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
3.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
1.
0.044 s
OK
2.
0.044 s
OK
1.
0.044 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.048 s
OK
3.
0.056 s
OK
large_1
chaotic + sequence 1, 2, ..., 40000 (without minus)
chaotic + sequence 1, 2, ..., 40000 (without minus)
✘
TIMEOUT ERROR
running time: 4.048 sec., time limit: 0.400 sec.
running time: 4.048 sec., time limit: 0.400 sec.
1.
4.048 s
TIMEOUT ERROR,
running time: 4.048 sec., time limit: 0.400 sec.
large_2
shuffled sequence 1, 2, ..., 100000 (without minus)
shuffled sequence 1, 2, ..., 100000 (without minus)
✘
TIMEOUT ERROR
Killed. Hard limit reached: 6.000 sec.
Killed. Hard limit reached: 6.000 sec.
1.
6.000 s
TIMEOUT ERROR,
Killed. Hard limit reached: 6.000 sec.
2.
6.000 s
TIMEOUT ERROR,
Killed. Hard limit reached: 6.000 sec.
1.
0.344 s
OK