Tasks Details
easy
1.
AbsDistinct
Compute number of distinct absolute values of sorted array elements.
Task Score
85%
Correctness
100%
Performance
33%
A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array.
For example, consider array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6The absolute distinct count of this array is 5, because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6.
Write a function:
int solution(vector<int> &A);
that, given a non-empty array A consisting of N numbers, returns absolute distinct count of array A.
For example, given array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6the function should return 5, as explained above.
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 [−2,147,483,648..2,147,483,647];
- array A is sorted in non-decreasing order.
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 10 minutes
Notes
not defined yet
Code: 12:51:39 UTC,
java,
autosave
Code: 12:51:45 UTC,
cpp,
autosave
Code: 12:52:06 UTC,
cpp,
autosave
Code: 12:52:36 UTC,
cpp,
autosave
Code: 12:53:02 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();
for(int i=0; i<len; i++){
}
}
Code: 12:53:13 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();
for(int i=0; i<len; i++){
A[i];
}
}
Code: 12:54:46 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();
for(int i=0; i<len; i++){
abA[i];
}
}
Code: 12:55:31 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;
v
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:55:42 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;
vector<int> check;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:55:56 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;
vector<int> check();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:57:42 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;
vector<int> le();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:57:54 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;
vector<int> absdistinct();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:58:04 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;
vector<int> absDistinct();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
Code: 12:58:24 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
if()
absDistinct.push_back()
}
}
Code: 12:58:36 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
abs(A[i])
if()
absDistinct.push_back()
}
}
Code: 12:58:49 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if()
absDistinct.push_back()
}
}
Code: 12:59:08 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find())
absDistinct.push_back()
}
}
Code: 12:59:38 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
}
Code: 13:00: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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)':
func.cpp:12:24: error: 'class std::vector<int>' has no member named 'find'; did you mean 'end'?
if(absDistinct.find(abs_val == absDistinct.end())){
^~~~
func.cpp:12:37: error: no match for 'operator==' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}')
if(absDistinct.find(abs_val == absDistinct.end())){
~~~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /opt/lang/gcc/include/c++/6.2.0/regex:62:0,
from /opt/lang/gcc/include/c++/6.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:110,
from func.cpp:2:
/opt/lang/gcc/include/c++/6.2.0/bits/regex.h:937:5: note: candidate: template<class _BiIter> bool std::__cxx11::operator==(const std::__cxx11::sub_match<_BiIter>&, const std::__cxx11::sub_match<_BiIter>&)
operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
^~~~~~~~
/opt/lang/gcc/include/c++/6.2.0/bits/regex.h:937:5: note: template argument deduction/substitution failed:
func.cpp:12:56: note: mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int'
if(absDistinct.find(abs_val == absDistinct.end())){
^
In file included from /opt/lang/gcc/include/c++/6.2.0/regex:62:0,
from /opt/lang/gcc/include/c++/6.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:110,
from func.cpp:2:
/opt/lang/gcc/include/c++/6.2.0/bits/regex.h:1010:5: note: candidate: template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator==(std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const std::__cxx11::sub_match<_BiIter>&)
operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
^~~~~~~~
/opt/lang/gcc/include/c++/6.2.0/bits/regex.h:1010:5: note: template argument deduction/substitution failed:
func.cpp:12:56: note: mismatched t
Code: 13:00: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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val) == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Code: 13:00:21 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)':
func.cpp:12:24: error: 'class std::vector<int>' has no member named 'find'; did you mean 'end'?
if(absDistinct.find(abs_val) == absDistinct.end()){
^~~~
Code: 13:01:07 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(v.begin(), v.end(), 20); == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Code: 13:01:20 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val); == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Code: 13:01:23 UTC,
cpp,
verify,
result: Passed
// 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Analysis
Code: 13:01:29 UTC,
cpp,
verify,
result: Passed
// 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Analysis
Code: 13:01:34 UTC,
cpp,
final,
score: 
85
// 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;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N*log(N))
expand all
Correctness tests
1.
0.003 s
OK
2.
0.003 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.003 s
OK
5.
0.003 s
OK
6.
0.003 s
OK
7.
0.003 s
OK
8.
0.003 s
OK
9.
0.004 s
OK
1.
0.003 s
OK
2.
0.003 s
OK
3.
0.004 s
OK
1.
0.003 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.003 s
OK
2.
0.003 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK