Check for each subarray whether it consists of all natural numbers up to its length or not | Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver Code
def checksubarrayExist1_N ( arr , N ) : NEW_LINE INDENT pos = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT pos [ arr [ i ] ] = i NEW_LINE DEDENT st = { } NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT st [ pos [ i ] ] = 1 NEW_LINE Min = sorted ( list ( st . keys ( ) ) ) [ 0 ] NEW_LINE Max = sorted ( list ( st . keys ( ) ) ) [ - 1 ] NEW_LINE if ( Max - Min + 1 == i ) : NEW_LINE INDENT print ( " True " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE checksubarrayExist1_N ( arr , N ) NEW_LINE DEDENT