Tasks Details
easy
1.
AbsDistinct
Compute number of distinct absolute values of sorted array elements.
Task Score
100%
Correctness
100%
Performance
100%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 13:14:35 UTC,
java,
autosave
Code: 13:14:46 UTC,
cpp,
autosave
Code: 13:14: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;
set<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:15:14 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;
set<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]);
set.insert(abs_val)
}
int res = absDistinct.size();
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:12:12: error: missing template arguments before '.' token set.insert(abs_val) ^
Code: 13:15:26 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;
set<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]);
absDistinct.insert(abs_val)
}
int res = absDistinct.size();
return res;
}
Code: 13:15:26 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;
set<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]);
absDistinct.insert(abs_val)
}
int res = absDistinct.size();
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:13:5: error: expected ';' before '}' token } ^
Code: 13:15:37 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;
set<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]);
absDistinct.insert(abs_val);
}
int res = absDistinct.size();
return res;
}
Analysis
Code: 13:15:58 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;
set<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]);
absDistinct.insert(abs_val);
}
int res = absDistinct.size();
return res;
}
Analysis
Code: 13:16:03 UTC,
cpp,
final,
score: 
100
// 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;
set<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]);
absDistinct.insert(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.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
5.
0.001 s
OK
6.
0.001 s
OK
7.
0.001 s
OK
8.
0.001 s
OK
9.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
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.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK