Problem Statement:
You are given a sorted array, you need to find the count of absolute distinct values in the array.
Example:
Input arr = [1, 2, 1, 2, -1, -2]
Output: 2
Explanation:
The count of absolute distinct count is 2.
Solution Explanation:
Solution is very simple.
We will insert all the elements of the array into set.
Code Solution
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <unordered_set>
using namespace std;
int solution(vector<int> arr)
{
unordered_set<int> set;
for (int i: arr)
{
set.insert(abs(i));
}
return set.size();
}
int main()
{
vector<int> input = { 1, 2, 1, 2, -1, -2};
cout << solution(input);
return 0;
}
Output
2