Given a sorted array of integers, return the number of distinct absolute values among the elements of the array. The input can contain duplicates values.
Input: [-3, -2, 0, 3, 4, 5] Output: 5 There are 5 distinct absolute values among the elements of this array, i.e. 0, 2, 3, 4 and 5) Input: [-1, -1, -1, -1, 0, 1, 1, 1, 1] Output: 2 Input: [-1, -1, -1, -1, 0] Output: 2 Input: [0, 0, 0] Output: 1
The solution should do only one scan of the input array and should not use any extra space. i.e. expected time complexity is O(n) and auxiliary space is O(1).
One simple solution is to use set. For each element of the input array, we insert its absolute value in the set. As set doesn’t support duplicate elements, the element’s absolute value will be inserted only once. Therefore, the required count is size of the set.
Below is the implementation of the idea.
Count of absolute distinct values : 3
Time Complexity : O(n)
Auxiliary Space : O(n)
The above implementation takes O(n) extra space, how to do in O(1) extra space?
The idea is to take advantage of the fact that the array is already Sorted. We initialize the count of distinct elements to number of elements in the array. We start with two index variables from two corners of the array and check for pair in the input array with sum as 0. If pair with 0 sum is found or duplicates are encountered, we decrement the count of distinct elements.Finally we return the updated count.
Below is the implementation of above approach.
# Python3 program to find absolute distinct
# count of an array using O(1) space.
# The function returns return number
# of distinct absolute values
# among the elements of the array
def distinctCount(arr, n):
# initialize count as number of elements
count = n;
i = 0; j = n – 1; sum = 0;
while (i < j): # Remove duplicate elements from the # left of the current window (i, j) # and also decrease the count while (i != j and arr[i] == arr[i + 1]): count = count - 1; i = i + 1; # Remove duplicate elements from the # right of the current window (i, j) # and also decrease the count while (i != j and arr[j] == arr[j - 1]): count = count - 1; j = j - 1; # break if only one element is left if (i == j): break; # Now look for the zero sum pair # in current window (i, j) sum = arr[i] + arr[j]; if (sum == 0): # decrease the count if (positive, # negative) pair is encountered count = count - 1; i = i + 1; j = j - 1; elif(sum < 0): i = i + 1; else: j = j - 1; return count; # Driver code arr = [-2, -1, 0, 1, 1]; n = len(arr); print("Count of absolute distinct values : ", distinctCount(arr, n)); # This code is contributed # by Akanksha Rai [tabby title="C#"]
Count of absolute distinct values : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Print sorted distinct elements of array
- Making elements distinct in a sorted array by minimum increments
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count maximum elements of an array whose absolute difference does not exceed K
- Count distinct elements in an array
- Count number of distinct pairs whose sum exists in the given array
- Count of distinct substrings of a string using Suffix Array
- Count subarrays having total distinct elements same as original array
- Sort an array according to the increasing count of distinct Prime Factors
- Count pairs in a sorted array whose sum is less than x
- Count pairs in a sorted array whose product is less than k
- Find the Rotation Count in Rotated Sorted array
- Count elements less than or equal to a given value in a sorted rotated array
- Count of smaller or equal elements in sorted array
- Count number of occurrences (or frequency) in a sorted array