Longest common subarray in the given two arrays | Function to find the maximum length of equal subarray ; Auxiliary dp [ ] [ ] array ; Updating the dp [ ] [ ] table in Bottom Up approach ; If A [ i ] is equal to B [ i ] then dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; Find maximum of all the values in dp [ ] [ ] array to get the maximum length ; Update the length ; Return the maximum length ; Driver Code ; Function call to find maximum length of subarray
def FindMaxLength ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT if A [ i ] == B [ j ] : NEW_LINE INDENT dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT DEDENT maxm = 0 NEW_LINE for i in dp : NEW_LINE INDENT for j in i : NEW_LINE INDENT maxm = max ( maxm , j ) NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 8 , 2 , 1 ] NEW_LINE B = [ 8 , 2 , 1 , 4 , 7 ] NEW_LINE print ( FindMaxLength ( A , B ) ) NEW_LINE DEDENT