Longest Common Increasing Subsequence ( LCS + LIS ) | Returns the length and the LCIS of two arrays arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] ; table [ j ] is going to store length of LCIS ending with arr2 [ j ] . We initialize it as 0 , ; Traverse all elements of arr1 [ ] ; Initialize current length of LCIS ; For each element of arr1 [ ] , trvarse all elements of arr2 [ ] . ; If both the array have same elements . Note that we don 't break the loop here. ; Now seek for previous smaller common element for current element of arr1 ; The maximum value in table [ ] is out result ; Driver Code
< ? php function LCIS ( $ arr1 , $ n , $ arr2 , $ m ) { $ table = Array ( ) ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) $ table [ $ j ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current = 0 ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ arr1 [ $ i ] == $ arr2 [ $ j ] ) if ( $ current + 1 > $ table [ $ j ] ) $ table [ $ j ] = $ current + 1 ; if ( $ arr1 [ $ i ] > $ arr2 [ $ j ] ) if ( $ table [ $ j ] > $ current ) $ current = $ table [ $ j ] ; } } $ result = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ table [ $ i ] > $ result ) $ result = $ table [ $ i ] ; return $ result ; } $ arr1 = array ( 3 , 4 , 9 , 1 ) ; $ arr2 = array ( 5 , 3 , 8 , 9 , 10 , 2 , 1 ) ; $ n = sizeof ( $ arr1 ) ; $ m = sizeof ( $ arr2 ) ; echo " Length ▁ of ▁ LCIS ▁ is ▁ " , LCIS ( $ arr1 , $ n , $ arr2 , $ m ) ; ? >