human
Can you provide a code to smallest element present in every subarray of all possible lengths and the other steps required given the following:
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"
Solution in Python:
assistant
#Here is code to smallest element present in every subarray of all possible lengths and the other steps required.
 def uniqueElements ( arr , start , K , mp ) :
	st = set ( ) ;
	for i in range ( K ) :
		st . add ( arr [ start + i ] ) ;
	for itr in st :
		if itr in mp :
			mp [ itr ] += 1 ;
		else :
			mp [ itr ] = 1 ;
def checkAnswer ( mp , N , K ) :
	for i in range ( 1 , N + 1 ) :
		if i in mp :
			if ( mp [ i ] == ( N - K + 1 ) ) :
				print ( i , end = " ▁ " ) ;
				return ;
	print ( - 1 , end = " ▁ " ) ;
def smallestPresentNumber ( arr , N , K ) :
	mp = { } ;
	for i in range ( N - K + 1 ) :
		uniqueElements ( arr , i , K , mp ) ;
	checkAnswer ( mp , N , K ) ;
def generateK ( arr , N ) :
	for k in range ( 1 , N + 1 ) :
		smallestPresentNumber ( arr , N , k ) ;
if __name__ == " _ _ main _ _ " :
	arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ;
	N = len ( arr ) ;
	generateK ( arr , N ) ;