Find the Longest Increasing Subsequence in Circular manner | Utility function to find LIS using Dynamic programming ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Set j on the basis of current window i . e . first element of the current window ; Pick maximum of all LIS values ; Function to find LIS in Circular manner ; Make a copy of given array by appending same array elements to itself ; Perform LIS for each window of size n ; Driver Code
< ? php function computeLIS ( $ circBuff , $ start , $ end , $ n ) { $ LIS = Array ( ) ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ LIS [ $ i ] = 1 ; for ( $ i = $ start + 1 ; $ i < $ end ; $ i ++ ) for ( $ j = $ start ; $ j < $ i ; $ j ++ ) if ( $ circBuff [ $ i ] > $ circBuff [ $ j ] && $ LIS [ $ i ] < $ LIS [ $ j ] + 1 ) $ LIS [ $ i ] = $ LIS [ $ j ] + 1 ; $ res = PHP_INT_MIN ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ res = max ( $ res , $ LIS [ $ i ] ) ; return $ res ; } function LICS ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ n ; $ i < 2 * $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i - $ n ] ; $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = max ( computeLIS ( $ circBuff , $ i , $ i + $ n , $ n ) , $ res ) ; return $ res ; } $ arr = array ( 1 , 4 , 6 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Length ▁ of ▁ LICS ▁ is ▁ " , LICS ( $ arr , $ n ) ; ? >