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 leto 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
function checksubarrayExist1_N ( arr , N ) { let pos = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { pos . set ( arr [ i ] , i ) ; } let st = new Set ( ) ; for ( let i = 1 ; i <= N ; i ++ ) { st . add ( pos . get ( i ) ) ; let Min = Math . min ( ... st ) ; let Max = Math . max ( ... st ) ; if ( Max - Min + 1 == i ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } let arr = [ 1 , 4 , 3 , 2 ] ; let N = arr . length ; checksubarrayExist1_N ( arr , N ) ;