Length of the largest subarray with contiguous elements | Set 1 | Utility functions to find minimum and maximum of two elements ; Returns length of the longest contiguous subarray ; Initialize result ; Initialize min and max for all subarrays starting with i ; Consider all subarrays starting with i and ending with j ; Update min and max in this subarray if needed ; If current subarray has all contiguous elements ; Return result ; Driver Code
< ? php function mins ( $ x , $ y ) { if ( $ x < $ y ) return $ x ; else return $ y ; } function maxi ( $ a , $ b ) { if ( $ a > $ b ) return $ a ; else return $ b ; } function findLength ( & $ arr , $ n ) { $ max_len = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ mn = $ arr [ $ i ] ; $ mx = $ arr [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ mn = mins ( $ mn , $ arr [ $ j ] ) ; $ mx = maxi ( $ mx , $ arr [ $ j ] ) ; if ( ( $ mx - $ mn ) == $ j - $ i ) $ max_len = maxi ( $ max_len , $ mx - $ mn + 1 ) ; } } return $ max_len ; } $ arr = array ( 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 ) ; $ n = sizeof ( $ arr ) ; echo ( " Length ▁ of ▁ the ▁ longest ▁ contiguous " . " ▁ subarray ▁ is ▁ " ) ; echo ( findLength ( $ arr , $ n ) ) ; ? >