Check if all subarrays contains at least one unique element | C ++ program for the above approach ; Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { map < int , int > hm ; int count = 0 ; for ( int j = i ; j < n ; j ++ ) { hm [ arr [ j ] ] ++ ; if ( hm [ arr [ j ] ] == 1 ) count ++ ; if ( hm [ arr [ j ] ] == 2 ) count -- ; if ( count == 0 ) return " No " ; } } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << check ( arr , N ) ; }