Absolute distinct count in a sorted array in C++?



An array is a data structure that is used to store elements of the same data type, where a sorted array, in which the elements are arranged in ascending or descending order.

The absolute distinct count gives the count of absolute (non-negative or unsigned) values of distinct (unique) elements present in an array. In this article, we will learn how to find the count of absolute values of all distinct elements in the given sorted array in C++.

Consider the following input and output scenarios to under the problem better:

Scenario 1

Input: [-5, -2, 1, 2, 5, 5, 6, 8] 
Output: 5
Explanation 
Here, we will first take the absolute (positive) values of all elements.
Absolute values: [5, 2, 1, 2, 5, 5, 6, 8]
Now, take out all the distinct (unique) values from it; therefore, we will be left with;
[1, 2, 5, 6, 8].
So, there are a total of 5 absolute distinct elements present in a given array.

Scenario 2

Input: [-2, 3, 0, 4, -4, -1, 7, 0]
Output: 6
Explanation
First, take the absolute (positive) values of all elements in the array:
Absolute values: [2, 3, 0, 4, 4, 1, 7, 0]
Now, take out all the distinct (unique) values from it; therefore, we will be left with;
[0, 1, 2, 3, 4, 7].
So, there are a total of 6 absolute distinct elements present in a given array.

To solve this problem, we will use two different methods.

Absolute Distinct Count in a Sorted Array Using set

A set is a container that is used to store unique elements in sorted order. It automatically detects duplicate elements and does not allow them.

Here are the steps to find the absolute distinct count in a sorted array using set:

  • Create an empty unordered set of the same data type as the array.
  • Traverse the array using a for loop from i = 0 to n - 1 and insert the absolute value of the array into set s using the abs() function.
  • The set will store a single (unique) value even if multiple same values are inserted.
  • Now, after all elements are inserted, return the length of the set.
  • This will give the number of absolute distinct elements present in the array.

Example

Here is the following example code of using a set data structure to get the absolute distinct values in a given array.

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {-3, 0, 2, 6};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout << "Count of absolute distinct values : ";
   unordered_set<int> s;
   for (int i = 0 ; i < n; i++)
      s.insert(abs(arr[i]));
      int count = s.size();
      cout<<count;
      return 0;
}

The output of the above code is:

Count of absolute distinct values : 4

Absolute Distinct Count in a Sorted Array Using Array Check

This is a method in which an array is used to access each element, and a counter variable, to keep track and to check the given condition.

Here are the steps to find the absolute distinct count in a sorted array using array check and counter variable:

  • Initialize a variable count = n (assuming all elements are distinct) and set two pointers i = 0 and j = n - 1.
  • Use while loops to check for duplicates from the left and then from the right, and reduce the count if found.
  • Now check for opposite values (like 5 and -5) by adding them, if it result 0, it means an opposite value pair exists, therefore reduce the count.

Example

Here is the following example code for finding out the distinct count in a sorted array using an array check and a count variable.

#include <iostream>
using namespace std;
int main() {
   int arr[] = {-5, -1, 0, 5, 8};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout << "Count of absolute distinct values : ";
   int count = n;
   int i = 0, j = n - 1, sum = 0;

   while (i < j) {
       // checking for duplicates from the left side and reducing count if found
      while (i != j && arr[i] == arr[i + 1])
         count--, i++;

      // checking for duplicates from the right side and reducing count if found
      while (i != j && arr[j] == arr[j - 1])
         count--, j--;

      if (i == j)   // pointer meets, no more pairs left to compare
         break;
 
      // this is to detect opposite values
      sum = arr[i] + arr[j];
      if (sum == 0) {
         count--;   // found opposite values, therefore count reduces
         i++, j--;
      }
      else if(sum < 0)
         i++;
      else
         j--;
   }
   cout<< count;
   return 0;
}

The output of the above code is:

Count of absolute distinct values : 4
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-07-22T15:53:47+05:30

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements