Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Absolute distinct count in a sorted array?
In this article, we will learn how to count the number of elements whose absolute values are distinct in an array. For example, in the array {5, 5, 6, -5, 8, 2, -2, 1}, there are 8 elements total, but only 5 distinct absolute values: {5, 6, 8, 2, 1}. Note that -5 and 5 are considered the same since their absolute values are identical.
Syntax
int absoluteDistinctCount(int arr[], int n);
Algorithm
To solve this problem, we use a simple approach −
1. Create a boolean array to mark visited absolute values 2. For each element in the array: - Calculate its absolute value - Mark this absolute value as visited 3. Count the number of marked positions
Example 1: Using Boolean Array Method
This approach uses a boolean array to track which absolute values we have encountered −
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int absoluteDistinctCount(int arr[], int n) {
int maxVal = 0;
// Find maximum absolute value to determine array size
for (int i = 0; i < n; i++) {
int absVal = abs(arr[i]);
if (absVal > maxVal) {
maxVal = absVal;
}
}
// Create boolean array to mark visited values
int *visited = (int*)calloc(maxVal + 1, sizeof(int));
if (visited == NULL) {
return -1; // Memory allocation failed
}
int count = 0;
for (int i = 0; i < n; i++) {
int absVal = abs(arr[i]);
if (!visited[absVal]) {
visited[absVal] = 1;
count++;
}
}
free(visited);
return count;
}
int main() {
int arr[] = {5, 5, 6, -5, 8, 2, -2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int result = absoluteDistinctCount(arr, n);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nAbsolute Distinct Count: %d<br>", result);
return 0;
}
Array: 5 5 6 -5 8 2 -2 1 Absolute Distinct Count: 5
Example 2: Using Sorting Approach
This method first converts all elements to their absolute values and sorts them to count distinct elements −
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int absoluteDistinctCountSorted(int arr[], int n) {
// Create a copy to avoid modifying original array
int *temp = (int*)malloc(n * sizeof(int));
if (temp == NULL) {
return -1;
}
// Convert to absolute values
for (int i = 0; i < n; i++) {
temp[i] = abs(arr[i]);
}
// Sort the array
qsort(temp, n, sizeof(int), compare);
// Count distinct elements
int count = 1;
for (int i = 1; i < n; i++) {
if (temp[i] != temp[i-1]) {
count++;
}
}
free(temp);
return count;
}
int main() {
int arr[] = {5, 5, 6, -5, 8, 2, -2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int result = absoluteDistinctCountSorted(arr, n);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nAbsolute Distinct Count: %d<br>", result);
return 0;
}
Array: 5 5 6 -5 8 2 -2 1 Absolute Distinct Count: 5
Comparison
| Method | Time Complexity | Space Complexity | Pros | Cons |
|---|---|---|---|---|
| Boolean Array | O(n) | O(max_value) | Fast, single pass | High space for large values |
| Sorting | O(n log n) | O(n) | Space efficient | Slower due to sorting |
Conclusion
Both methods effectively count distinct absolute values in an array. The boolean array method is faster but uses more space, while the sorting method is more space-efficient for arrays with large values.
