human
Length of largest common subarray in all the rows of given Matrix | Function to find longest common subarray in all the rows of the matrix.
Array to store the position of element in every row.
Traverse the matrix.
Store the position of every element in every row.
Variable to store length of largest common Subarray.
Traverse through the matrix column.
Variable to check if every row has arr [ i ] [ j ] next to arr [ i - 1 ] [ j ] or not.
Traverse through the matrix rows.
Check if arr [ i ] [ j ] is next to arr [ i ] [ j - 1 ] in every row or not.
If every row has arr [ 0 ] [ j ] next to arr [ 0 ] [ j - 1 ] increment len by 1 and update the value of ans.
Driver Code.
Given Input.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to length of largest common subarray in all the rows of given Matrix and the other steps required.
 def largestCommonSubarray ( arr , n , m ) :
	dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n ) ]
	for i in range ( n ) :
		for j in range ( m ) :
			dp [ i ] [ arr [ i ] [ j ] ] = j
	ans = 1
	len1 = 1
	for i in range ( 1 , m , 1 ) :
		check = True
		for j in range ( 1 , n , 1 ) :
			if ( dp [ j ] [ arr [ 0 ] [ i - 1 ] ] + 1 != dp [ j ] [ arr [ 0 ] [ i ] ] ) :
				check = False
				break
		if ( check ) :
			len1 += 1
			ans = max ( ans , len1 )
		else :
			len1 = 1
	return ans
if __name__ == ' _ _ main _ _ ' :
	n = 4
	m = 5
	arr = [ [ 4 , 5 , 1 , 2 , 3 , 6 , 7 ] , [ 1 , 2 , 4 , 5 , 7 , 6 , 3 ] , [ 2 , 7 , 3 , 4 , 5 , 1 , 6 ] ]
	N = len ( arr )
	M = len ( arr [ 0 ] )
	print ( largestCommonSubarray ( arr , N , M ) )