Smallest element present in every subarray of all possible lengths | Function to add count of numbers in the map for a subarray of length k ; Set to store unique elements ; add elements to the set ; Adding count in map ; Function to check if there is any number which repeats itself in every subarray of length K ; Check all number starting from 1 ; Check if i occurred n - k + 1 times ; Print the smallest number ; Print - 1 , if no such number found ; Function to count frequency of each number in each subarray of length K ; Traverse all subarrays of length K ; Check and print the smallest number present in every subarray and print it ; Function to generate the value of K ; Function call ; Given array ; Size of array ; Function call
function uniqueElements ( arr , start , K , mp ) { let st = new Set ( ) ; for ( let i = 0 ; i < K ; i ++ ) st . add ( arr [ start + i ] ) ; for ( let itr of st ) { if ( mp . has ( itr ) ) { mp . set ( itr , mp . get ( itr ) + 1 ) ; } else { mp . set ( itr , 1 ) ; } } } function checkAnswer ( mp , N , K ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( mp . has ( i ) ) if ( mp . get ( i ) == ( N - K + 1 ) ) { document . write ( i + " " ) ; return ; } } document . write ( - 1 + " " ) ; } function smallestPresentNumber ( arr , N , K ) { let mp = new Map ( ) ; for ( let i = 0 ; i <= N - K ; i ++ ) { uniqueElements ( arr , i , K , mp ) ; } checkAnswer ( mp , N , K ) ; } function generateK ( arr , N ) { for ( let k = 1 ; k <= N ; k ++ ) smallestPresentNumber ( arr , N , k ) ; } let arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; let N = arr . length ; generateK ( arr , N ) ;