Count of subarrays of an Array having all unique digits | C ++ program to find the count of subarrays of an Array having all unique digits ; Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < int > & v ) { set < int > digits ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { set < int > d ; while ( v [ i ] ) { d . insert ( v [ i ] % 10 ) ; v [ i ] /= 10 ; } for ( auto it : d ) { if ( digits . count ( it ) ) return false ; } for ( auto it : d ) digits . insert ( it ) ; } return true ; } int numberOfSubarrays ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { vector < int > temp ; for ( int j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) temp . push_back ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } int main ( ) { int N = 4 ; int A [ ] = { 1 , 12 , 23 , 34 } ; cout << numberOfSubarrays ( A , N ) ; return 0 ; }