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 ; Driver Code ; Given array ; Size of array ; Function call
def uniqueElements ( arr , start , K , mp ) : NEW_LINE INDENT st = set ( ) ; NEW_LINE for i in range ( K ) : NEW_LINE INDENT st . add ( arr [ start + i ] ) ; NEW_LINE DEDENT for itr in st : NEW_LINE INDENT if itr in mp : NEW_LINE INDENT mp [ itr ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT mp [ itr ] = 1 ; NEW_LINE DEDENT DEDENT DEDENT def checkAnswer ( mp , N , K ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i in mp : NEW_LINE INDENT if ( mp [ i ] == ( N - K + 1 ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT print ( - 1 , end = " ▁ " ) ; NEW_LINE DEDENT def smallestPresentNumber ( arr , N , K ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT uniqueElements ( arr , i , K , mp ) ; NEW_LINE DEDENT checkAnswer ( mp , N , K ) ; NEW_LINE DEDENT def generateK ( arr , N ) : NEW_LINE INDENT for k in range ( 1 , N + 1 ) : NEW_LINE INDENT smallestPresentNumber ( arr , N , k ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE generateK ( arr , N ) ; NEW_LINE DEDENT