Tasks Details
easy
1.
PermCheck
Check whether array A is a permutation.
Task Score
88%
Correctness
100%
Performance
80%
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:
int solution(vector<int> &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 C++
Time spent on task 40 minutes
Notes
not defined yet
Code: 06:09:25 UTC,
cpp,
verify,
result: Failed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n + 1];
memset(hash, 0, 4*(n + 1));
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n + 1);
}
for (int i = 1; i < n + 1; ++i)
{
if (*(hash + i) != 1)
return 0;
}
return 1;
}
Analysis
Code: 06:19:34 UTC,
cpp,
verify,
result: Failed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n + 1];
memset(hash, 0, 4*(n + 1));
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n + 1);
}
for (int i = 1; i < n + 1; ++i)
{
if (*(hash + i) != 1)
return 0;
}
return 1;
}
Analysis
Code: 06:19:39 UTC,
cpp,
verify,
result: Failed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n + 1];
memset(hash, 0, 4*(n + 1));
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n + 1);
}
for (int i = 1; i < n + 1; ++i)
{
if (*(hash + i) != 1)
return 0;
}
return 1;
}
Analysis
Code: 06:32:48 UTC,
cpp,
verify,
result: Failed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n];
memset(hash, 0, 4*(n));
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n) + 1;
}
for (int i = 0; i < n; ++i)
{
if (*(hash + i) == 0)
return 0;
}
return 1;
}
Analysis
Code: 06:35:06 UTC,
cpp,
verify,
result: Passed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n];
memset(hash, 0, 4*(n));
int max = 0;
for (int i = 0; i < n; ++i)
if (A[i] > max)
max = A[i];
if (max != n)
return 0;
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n) + 1;
}
for (int i = 0; i < n; ++i)
{
if (*(hash + i) == 0)
return 0;
}
return 1;
}
Analysis
Code: 06:35:54 UTC,
cpp,
verify,
result: Passed
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n];
memset(hash, 0, 4*(n));
int max = 0;
for (int i = 0; i < n; ++i)
if (A[i] > max)
max = A[i];
if (max != n)
return 0;
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n) + 1;
}
for (int i = 0; i < n; ++i)
{
if (*(hash + i) == 0)
return 0;
}
return 1;
}
Analysis
Code: 06:35:59 UTC,
cpp,
final,
score: 
88
// you can also use includes, for example:
// #include <algorithm>
#include<string.h>
int solution(vector<int> &A) {
// write your code in C++11
int n = A.size();
int* hash = new int[n];
memset(hash, 0, 4*(n));
int max = 0;
for (int i = 0; i < n; ++i)
if (A[i] > max)
max = A[i];
if (max != n)
return 0;
for (int i = 0; i < n; ++i)
{
*(hash + i) = A[i] % (n) + 1;
}
for (int i = 0; i < n; ++i)
{
if (*(hash + i) == 0)
return 0;
}
return 1;
}
Analysis summary
The following issues have been detected: wrong answers.
Analysis
Detected time complexity:
O(N * log(N)) or O(N)
expand all
Performance tests
1.
0.008 s
OK
antiSum2
total sum is corret (equals 1 + 2 + ... N), but it is not a permutation, N = ~100,000
total sum is corret (equals 1 + 2 + ... N), but it is not a permutation, N = ~100,000
✘
WRONG ANSWER
got 1 expected 0
got 1 expected 0
1.
0.044 s
WRONG ANSWER,
got 1 expected 0
1.
0.044 s
OK
1.
0.044 s
OK
1.
0.032 s
OK