Check if all subarrays contains at least one unique element | Java 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
import java . util . * ; import java . lang . * ; class GFG { static String check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Map < Integer , Integer > hm = new HashMap < > ( ) ; int count = 0 ; for ( int j = i ; j < n ; j ++ ) { hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . get ( arr [ j ] ) == 1 ) count ++ ; if ( hm . get ( arr [ j ] ) == 2 ) count -- ; if ( count == 0 ) return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 } ; int N = arr . length ; System . out . println ( check ( arr , N ) ) ; } }