Smallest element present in every subarray of all possible lengths | Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths Initialize with - 1. ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Given array ; Size of array ; Function Call
def printAnswer ( answer , N ) : NEW_LINE INDENT for i in range ( N + 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def updateAnswerArray ( answer , N ) : NEW_LINE INDENT i = 0 NEW_LINE while ( answer [ i ] == - 1 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT minimum = answer [ i ] NEW_LINE while ( i <= N ) : NEW_LINE INDENT if ( answer [ i ] == - 1 ) : NEW_LINE INDENT answer [ i ] = minimum NEW_LINE DEDENT else : NEW_LINE INDENT answer [ i ] = min ( minimum , answer [ i ] ) NEW_LINE DEDENT minimum = min ( minimum , answer [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def lengthOfSubarray ( indices , st , N ) : NEW_LINE INDENT answer = [ - 1 for i in range ( N + 1 ) ] NEW_LINE for itr in st : NEW_LINE INDENT start = - 1 NEW_LINE gap = - 1 NEW_LINE indices [ itr ] . append ( N ) NEW_LINE for i in range ( len ( indices [ itr ] ) ) : NEW_LINE INDENT gap = max ( gap , indices [ itr ] [ i ] - start ) NEW_LINE start = indices [ itr ] [ i ] NEW_LINE DEDENT if ( answer [ gap ] == - 1 ) : NEW_LINE INDENT answer [ gap ] = itr NEW_LINE DEDENT DEDENT updateAnswerArray ( answer , N ) NEW_LINE printAnswer ( answer , N ) NEW_LINE DEDENT def smallestPresentNumber ( arr , N ) : NEW_LINE INDENT indices = [ [ ] for i in range ( N + 1 ) ] NEW_LINE elements = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT indices [ arr [ i ] ] . append ( i ) NEW_LINE elements . append ( arr [ i ] ) NEW_LINE DEDENT elements = list ( set ( elements ) ) NEW_LINE lengthOfSubarray ( indices , elements , N ) NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE smallestPresentNumber ( arr , N ) NEW_LINE