Tasks Details
easy
1.
Distinct
Compute number of distinct values in an array.
Task Score
100%
Correctness
100%
Performance
100%
Write a function
int solution(vector<int> &A);
that, given an array A consisting of N integers, returns the number of distinct values in array A.
For example, given array A consisting of six elements such that:
A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..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++
Time spent on task 3 minutes
Notes
not defined yet
Task timeline
Code: 15:00:42 UTC,
java,
autosave
Code: 15:00:44 UTC,
cpp,
autosave
Code: 15:00:49 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001];
int arr_minus[1000001];
for(int i = 0; i < 1000000; i++){
arr_plus[i] = i;
arr_minus[i + 1] = -1 * (i + 1);
}
arr_plus[1000000] = 1000000;
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] != 2222222){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:01:16 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001];
int arr_minus[1000001];
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] != 2222222){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:01:20 UTC,
java,
autosave
Code: 15:01:23 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001];
int arr_minus[1000001];
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] != 2222222){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:01:27 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001];
int arr_minus[1000001];
for(int i = 0; i < 1000000; i++){
arr_plus[i] = i;
arr_minus[i + 1] = -1 * (i + 1);
}
arr_plus[1000000] = 1000000;
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] != 2222222){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:01:51 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] != 2222222){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:02:12 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = 2222222;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:02:22 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = num;
}
}else{
if(arr_minus[num * -1] != 2222222){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:02:33 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = num;
}
}else{
if(arr_minus[num * -1] == '\0'){
count++;
arr_minus[num * -1] = 2222222;
}
}
});
return count;
}
Code: 15:02:43 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = num;
}
}else{
if(arr_minus[num * -1] == '\0'){
count++;
arr_minus[num * -1] = num;
}
}
});
return count;
}
Code: 15:03:06 UTC,
cpp,
autosave
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = 1;
}
}else{
if(arr_minus[num * -1] == '\0'){
count++;
arr_minus[num * -1] = 1;
}
}
});
return count;
}
Code: 15:03:16 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = 1;
}
}else{
if(arr_minus[num * -1] == '\0'){
count++;
arr_minus[num * -1] = 1;
}
}
});
return count;
}
Analysis
Code: 15:03:19 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
#include <algorithm>
// 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 count = 0;
int arr_plus[1000001] = {'\0',};
int arr_minus[1000001] = {'\0',};
for_each(A.begin(), A.end(),[&](auto& num){
if(num >= 0){
if(arr_plus[num] == '\0'){
count++;
arr_plus[num] = 1;
}
}else{
if(arr_minus[num * -1] == '\0'){
count++;
arr_minus[num * -1] = 1;
}
}
});
return count;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N)) or O(N)
expand all
Correctness tests
1.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.004 s
OK
1.
0.004 s
OK