Length of largest common subarray in all the rows of given Matrix
Last Updated :
23 Jul, 2021
Given a matrix mat[][] of size N×M where each row of the matrix is a permutation of the elements from [1, M], the task is to find the maximum length of the subarray present in each row of the matrix.
Examples:
Input: mat[][] = {{1, 2, 3, 4, 5}, {2, 3, 4, 1, 5}, {5, 2, 3, 4, 1}, {1, 5, 2, 3, 4}}
Output: 3
Explanation:
In each row, {2, 3, 4} is the longest common sub-array present in all the rows of the matrix.
Input: mat[][] = {{4, 5, 1, 2, 3, 6, 7}, {1, 2, 4, 5, 7, 6, 3}, {2, 7, 3, 4, 5, 1, 6}}
Output: 2
Naive Approach: The simplest way to solve the problem is to generate all the possible subarray of the first row of the matrix and then check if the remaining rows contain that sub-array or not.
Time Complexity: O(M×N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by creating a matrix, say dp[][] that stores the position of the element in every row and then check if the current and previous elements index in each row has a difference of 1 or not. Follow the steps below to solve the problem:
- Initialize a matrix, say dp[][] that stores the position of every element in every row.
- To fill the matrix dp[][], Iterate in the range [0, N-1] using the variable i and perform the following steps:
- Initialize variable, say ans that stores the length of the longest sub-array common in all the rows and len that stores the length of the sub-array common in all the rows.
- Iterate in the range [1, M-1] using the variable i and perform the following steps:
- Initialize a boolean variable check as 1, that checks that whether a[i][0] comes after a[i-1][0] in each row or not.
- Iterate in the range [1, N-1] using the variable j and perform the following steps:
- If dp[j][arr[0][i-1]] + 1 != dp[j][arr[0][i]], modify the value of check as 0 and terminate the loop.
- If the value of the check is 1, then increment the value of len by 1 and modify the value of ans as max(ans, len).
- If the value of the check is 0, then modify the value of len as 1.
- After completing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestCommonSubarray(
vector<vector< int > > arr,
int n, int m)
{
int dp[n][m + 1];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
dp[i][arr[i][j]] = j;
}
}
int ans = 1;
int len = 1;
for ( int i = 1; i < m; i++) {
bool check = true ;
for ( int j = 1; j < n; j++) {
if (dp[j][arr[0][i - 1]] + 1
!= dp[j][arr[0][i]]) {
check = false ;
break ;
}
}
if (check) {
len++;
ans = max(ans, len);
}
else {
len = 1;
}
}
return ans;
}
int main()
{
int n = 4;
int m = 5;
vector<vector< int > > arr{ { 4, 5, 1, 2, 3, 6, 7 },
{ 1, 2, 4, 5, 7, 6, 3 },
{ 2, 7, 3, 4, 5, 1, 6 } };
int N = arr.size();
int M = arr[0].size();
cout << largestCommonSubarray(arr, N, M);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int largestCommonSubarray( int [][] arr,
int n, int m)
{
int dp[][] = new int [n][m + 1 ];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
dp[i][arr[i][j]] = j;
}
}
int ans = 1 ;
int len = 1 ;
for ( int i = 1 ; i < m; i++)
{
boolean check = true ;
for ( int j = 1 ; j < n; j++)
{
if (dp[j][arr[ 0 ][i - 1 ]] + 1 !=
dp[j][arr[ 0 ][i]])
{
check = false ;
break ;
}
}
if (check)
{
len++;
ans = Math.max(ans, len);
}
else
{
len = 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 4 ;
int m = 5 ;
int [][] arr = { { 4 , 5 , 1 , 2 , 3 , 6 , 7 },
{ 1 , 2 , 4 , 5 , 7 , 6 , 3 },
{ 2 , 7 , 3 , 4 , 5 , 1 , 6 } };
int N = arr.length;
int M = arr[ 0 ].length;
System.out.println(largestCommonSubarray(arr, N, M));
}
}
|
Python3
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))
|
Javascript
<script>
function largestCommonSubarray(arr, n, m) {
let dp = Array(n).fill().map(() => Array(m + 1));
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
dp[i][arr[i][j]] = j;
}
}
let ans = 1;
let len = 1;
for (let i = 1; i < m; i++) {
let check = true ;
for (let j = 1; j < n; j++) {
if (dp[j][arr[0][i - 1]] + 1
!= dp[j][arr[0][i]]) {
check = false ;
break ;
}
}
if (check) {
len++;
ans = Math.max(ans, len);
}
else {
len = 1;
}
}
return ans;
}
let n = 4;
let m = 5;
let arr = [[4, 5, 1, 2, 3, 6, 7],
[1, 2, 4, 5, 7, 6, 3],
[2, 7, 3, 4, 5, 1, 6]];
let N = arr.length;
let M = arr[0].length;
document.write(largestCommonSubarray(arr, N, M));
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int largestCommonSubarray( int [,]arr,
int n, int m)
{
int [,]dp = new int [n,m + 1];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
dp[i,arr[i,j]] = j;
}
}
int ans = 1;
int len = 1;
for ( int i = 1; i < m; i++)
{
bool check = true ;
for ( int j = 1; j < n; j++)
{
if (dp[j,arr[0,i - 1]] + 1 !=
dp[j,arr[0,i]])
{
check = false ;
break ;
}
}
if (check == true )
{
len++;
ans = Math.Max(ans, len);
}
else
{
len = 1;
}
}
return ans;
}
public static void Main()
{
int [,]arr = { { 4, 5, 1, 2, 3, 6, 7 },
{ 1, 2, 4, 5, 7, 6, 3 },
{ 2, 7, 3, 4, 5, 1, 6 } };
int N = 3;
int M = 7;
Console.Write(largestCommonSubarray(arr, N, M));
}
}
|
Time Complexity: O(N×M)
Auxiliary Space: O(N×M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...