If distinct elements equals to k then try to increment left side .
while ( hm . size == k ) { if ( hm . has ( arr [ i ] ) && hm . get ( arr [ i ] ) == 1 ) hm . delete ( arr [ i ] ) ; else if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) - 1 ) ;