It is same as explained in above loop .
if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; }