Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count subarrays having total distinct elements same as original array in C++
We are given an array arr[] containing integers. The goal is to count all subarrays of arr[] such that the number of distinct elements in each is the same as the number of distinct elements in the original array. If the original array is [1,1,2,3] then subarrays will be [1,2,3] and [1,1,2,3].
Total distinct elements in the original array is 3. Total distinct elements in both subarrays is also 3
Let us understand with examples.
Input − arr[] = {1,2,1,2,3,4,2 };
Output − Count of subarrays having total distinct elements same as an original array is − 6
Explanation − Distinct elements in arr[] are 4 (1,2,3,4). Subarrays with the same number of distinct elements are: ( count distinct from left to right )
[1,2,1,2,3,4], [2,1,2,3,4], [1,2,3,4], [1,2,3,4,2], [2,1,2,3,4,2], [1,2,1,2,3,4,2 ]
Input − arr[] = {8,7,5,6,10 };
Output − Count of subarrays having total distinct elements same as an original array is − 1
Explanation − Distinct elements in arr[] are 5 (5,6,7,8,10). Subarrays with same number of distinct elements are: ( count distinct from left to right ) [8,7,6,5,10]. 1 only
The approach used in the below program is as follows
Take an array arr[] of integer numbers and calculate the size of an array.
Function sub_ele_diff_one(int arr[], int size) takes the array and returns a count of subarrays with consecutive elements differing by 1.
Take a temporary variable count and variables right and left.
Take a variable of type unordered_map for creating the random pairs.
Start loop FOR from 0 till the size of an array and inside it set the value of arr[i] inside the unordered_map.
Now, calculate the size of an unordered_map and clear the unordered_map.
Start loop FOR from 0 till the size of an array.
Inside the loop, start WHILE till right < size and left < size of an unordered_map
Pre-Increment the value of um[arr[right]]
Now, check if um[arr[right]] = 1 then pre increment the value of left by 1.
Outside the WHILE, pre-increment the value of right by 1.
Check IF left = size of an unordered_map then set count to count + size - right + 1
Decrement the um[arr[i]] by 1
Check IF um[arr[i]] = 0 then decrement the left by 1
Return the count
Print the result.
Example
#include <bits/stdc++.h>
using namespace std;
int sub_distinct(int arr[], int size){
int count = 0, right = 0, left = 0;
unordered_map<int, int> um;
for (int i = 0; i < size; ++i){
um[arr[i]] = 1;
}
int um_size = um.size();
um.clear();
for(int i = 0; i < size; ++i){
while (right < size && left < um_size){
++um[arr[right]];
if (um[arr[right]] == 1){
++left;
}
++right;
}
if (left == um_size){
count = count + (size - right + 1);
}
--um[arr[i]];
if (um[arr[i]] == 0){
--left;
}
}
return count;
}
int main(){
int arr[] = {4, 3, 2, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"Count of subarrays having total distinct elements same as original array are: "<<sub_distinct(arr, size);
return 0;
}
Output
If we run the above code it will generate the following output −
Count of subarrays having total distinct elements same as original array are: 1