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:
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 9 minutes
Notes
not defined yet
Task timeline
Code: 03:49:36 UTC,
java,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long sum_tot = len*(len+1)/2;
long long sum_A = 0;
//
if(len == 1){
if(A[0] == 1){
return 1;
}else{
return 0;
}
}
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt ++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:49:58 UTC,
java,
autosave
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long sum_tot = len*(len+1)/2;
long long sum_A = 0;
//
if(len == 1){
if(A[0] == 1){
return 1;
}else{
return 0;
}
}
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt ++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:50:15 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
//
if(len == 1){
if(A[0] == 1){
return 1;
}else{
return 0;
}
}
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt ++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:50:28 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
//
if(len == 1){
if(A[0] == 1){
return 1;
}else{
return 0;
}
}
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:52:39 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:52:50 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:55:06 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:55:25 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
vector<int> check(len+1, 0);
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:56:06 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
// 전체 sum_tot
long long int sum_tot = len*(len+1)/2;
long long int sum_A = 0;
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:56:17 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
sum_A += A[i];
if(check[A[i]] == 0){
check[A[i]] = 1;
cnt++;
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:56:30 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(check[i] == 0){
}
}
if((sum_A == sum_tot) && (cnt == len)){
return 1;
}else{
return 0;
}
}
Code: 03:56:53 UTC,
cpp,
verify,
result: Failed
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
int cnt = 0;
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(check[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Analysis
Compile error
func.cpp:1:1: error: expected unqualified-id before '/' token / you can use includes, for example: ^ func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:16:12: error: 'check' was not declared in this scope if(check[i] == i+1){ ^~~~~ func.cpp:10:9: warning: unused variable 'cnt' [-Wunused-variable] int cnt = 0; ^~~
Code: 03:57:01 UTC,
cpp,
autosave
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(check[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Code: 03:57:03 UTC,
cpp,
verify,
result: Failed
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(check[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Analysis
Compile error
func.cpp:1:1: error: expected unqualified-id before '/' token / you can use includes, for example: ^ func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:15:12: error: 'check' was not declared in this scope if(check[i] == i+1){ ^~~~~
Code: 03:57:09 UTC,
cpp,
verify,
result: Failed
/ you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(A[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Analysis
Compile error
func.cpp:1:1: error: expected unqualified-id before '/' token / you can use includes, for example: ^
Code: 03:57:22 UTC,
cpp,
verify,
result: Passed
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(A[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Analysis
Code: 03:57:29 UTC,
cpp,
verify,
result: Passed
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(A[i] == i+1){
continue;
}else{
return 0;
}
}
return 1;
}
Analysis
Code: 03:57:34 UTC,
cpp,
final,
score: 
100
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
// sort
sort(A.begin(), A.end());
for(int i=0; i<len; i++){
if(A[i] == i+1){
continue;
}else{
return 0;
}
}
return 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.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
permutations_of_ranges
permutations of sets like [2..100] for which the anwsers should be false
permutations of sets like [2..100] for which the anwsers should be false
✔
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
expand all
Performance tests
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.008 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.001 s
OK
2.
0.004 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.012 s
OK
5.
0.012 s
OK