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:
class Solution { public int solution(int[] 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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Total time used 1 minutes
Effective time used 1 minutes
Notes
not defined yet
Task timeline
Code: 07:49:17 UTC,
java,
autosave
Code: 07:49:38 UTC,
cs,
autosave
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
Code: 07:49:54 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
A = A.Where(a => a > 0).Distinct().OrderBy(c => c).ToArray();
if (A.Length== 0)
{
return 1;
}
for (int i = 0; i < A.Length; i++)
{
Console.WriteLine(i + "=>" + A[i]);
if (i + 1 != A[i])
{
return i + 1;
}
}
return A.Max() + 1;
}
}
Analysis
Compile error
Compilation failed: 2 error(s), 0 warnings Solution.cs(10,19): error CS1061: Type `int[]' does not contain a definition for `Where' and no extension method `Where' of type `int[]' could be found. Are you missing `System.Linq' using directive? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(24,22): error CS1061: Type `int[]' does not contain a definition for `Max' and no extension method `Max' of type `int[]' could be found. Are you missing `System.Linq' using directive? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
Code: 07:50:05 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
A = A.Where(a => a > 0).Distinct().OrderBy(c => c).ToArray();
if (A.Length== 0)
{
return 1;
}
for (int i = 0; i < A.Length; i++)
{
Console.WriteLine(i + "=>" + A[i]);
if (i + 1 != A[i])
{
return i + 1;
}
}
return A.Max() + 1;
}
}
Analysis
expand all
Example tests
1.
0.040 s
OK
stdout:
0=>1 1=>2 2=>3 3=>4 4=>6
1.
0.044 s
OK
stdout:
0=>1 1=>2 2=>3
1.
0.036 s
OK
Code: 07:50:14 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
A = A.Where(a => a > 0).Distinct().OrderBy(c => c).ToArray();
if (A.Length== 0)
{
return 1;
}
for (int i = 0; i < A.Length; i++)
{
//Console.WriteLine(i + "=>" + A[i]);
if (i + 1 != A[i])
{
return i + 1;
}
}
return A.Max() + 1;
}
}
Analysis
Code: 07:50:16 UTC,
cs,
final,
score: 
100
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
A = A.Where(a => a > 0).Distinct().OrderBy(c => c).ToArray();
if (A.Length== 0)
{
return 1;
}
for (int i = 0; i < A.Length; i++)
{
//Console.WriteLine(i + "=>" + A[i]);
if (i + 1 != A[i])
{
return i + 1;
}
}
return A.Max() + 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.028 s
OK
2.
0.028 s
OK
3.
0.028 s
OK
4.
0.028 s
OK
1.
0.028 s
OK
2.
0.028 s
OK
3.
0.028 s
OK
1.
0.028 s
OK
2.
0.028 s
OK
1.
0.028 s
OK
2.
0.028 s
OK
1.
0.028 s
OK
expand all
Performance tests
1.
0.032 s
OK
2.
0.032 s
OK
3.
0.036 s
OK
1.
0.080 s
OK
1.
0.104 s
OK
2.
0.104 s
OK
1.
0.060 s
OK