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:
def solution(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 Python
Time spent on task 1 minutes
Notes
not defined yet
Code: 14:41:08 UTC,
java,
autosave
Code: 14:41:18 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 15:Caterpillar method
# P 15.1 AbsDistinct
def solution(A):
"""
计算数组A中绝对值不同的数字的个数
时间复杂度:O(N) or O(N*log(N))
:param A: 数组
:return: 绝对值不同的数字的个数
"""
return len(set(abs(i) for i in A))
Analysis
Code: 14:41:20 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 15:Caterpillar method
# P 15.1 AbsDistinct
def solution(A):
"""
计算数组A中绝对值不同的数字的个数
时间复杂度:O(N) or O(N*log(N))
:param A: 数组
:return: 绝对值不同的数字的个数
"""
return len(set(abs(i) for i in A))
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N*log(N))
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
7.
0.036 s
OK
8.
0.036 s
OK
9.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK