Count of subarrays of an Array having all unique digits | Java 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
import java . util . * ; class GFG { static boolean check ( Vector < Integer > v ) { HashSet < Integer > digits = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { HashSet < Integer > d = new HashSet < Integer > ( ) ; while ( v . get ( i ) > 0 ) { d . add ( v . get ( i ) % 10 ) ; v . set ( i , v . get ( i ) / 10 ) ; } for ( int it : d ) { if ( digits . contains ( it ) ) return false ; } for ( int it : d ) digits . add ( it ) ; } return true ; } static int numberOfSubarrays ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { Vector < Integer > temp = new Vector < Integer > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) temp . add ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 1 , 12 , 23 , 34 } ; System . out . print ( numberOfSubarrays ( A , N ) ) ; } }