Skip to content

Commit 2f40ec9

Browse files
committed
6. Maximum Length of Repeated Subarray (HASHING)
1 parent eab4697 commit 2f40ec9

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
// Maximum Length of Repeated Subarray
3+
4+
/*
5+
6+
Question : We will be given two array A and B of any size, and we need to compute the Maximum length of subarray which is present in both.
7+
8+
Algorithm :
9+
We will use hashing algorithm to solve this problem.
10+
First will make a hash table for array A, to store the index of each element of A.
11+
Then for each element of array B, will loop through the size of the hash value of that array B element and update the ans accordingly
12+
13+
*/
14+
15+
16+
#include<bits/stdc++.h>
17+
using namespace std;
18+
19+
int max_length_of_repeated_subarray(int a[ ], int b[ ], int n, int m)
20+
{
21+
// ans variable defined to store the answer of max length and initialized to 0
22+
int ans = 0;
23+
// unordered map taken for storing hash value of array a
24+
unordered_map<int, vector<int>> hash_a;
25+
for (int i=0;i<n;i++)
26+
{
27+
hash_a[a[i]].push_back(i); // stored indices of each array element in hashtable
28+
}
29+
30+
// Now we loop through all the element of array b
31+
for (int i=0;i<m;i++)
32+
{
33+
// and for each element of B, we loop through the value of size of hash value for that value
34+
for (int j = 0; j < hash_a[b[i]].size(); j++)
35+
{
36+
int k = 0;
37+
// for each of i and j we check the condition for maximum subarray length
38+
while (i+k<m && hash_a[b[i]][j] + k<n && b[i+k]==a[hash_a[b[i]][j]+k])
39+
{
40+
k++;// and if satisfies will keep on updating the values of k
41+
}
42+
// updating the ans if k we got is greater than previous ans
43+
if(k>ans)
44+
ans = k;
45+
}
46+
}
47+
return ans;//returning the final answer
48+
}
49+
50+
int main()
51+
{
52+
// declared two variable n and m, for size of both array
53+
int n,m;
54+
// taking input of both array size
55+
cin>>n>>m;
56+
// declared two array a and b of size n and m respectively.
57+
int a[n], b[m];
58+
// taking input of the element in array a
59+
for(int i=0;i<n;i++)
60+
{
61+
cin>>a[i];
62+
}
63+
// taking input of the element in array b
64+
for(int i=0;i<m;i++)
65+
{
66+
cin>>b[i];
67+
}
68+
69+
// function called for finding the maximum lenght of repeated subarray
70+
cout<<"The Maximum length of repeated subarray is : "<<max_length_of_repeated_subarray(a,b,n,m)<<endl;
71+
72+
return 0;
73+
}
74+
75+
/*
76+
77+
Time Complexity : O(n*m*k)
78+
Space Complexity : O(n)
79+
80+
Sample testcase :
81+
Input 1 :
82+
5
83+
5
84+
10 23 17 2 3
85+
23 17 4 5 19
86+
Output 1:
87+
The Maximum length of repeated subarray is : 2
88+
89+
Input 2 :
90+
5
91+
4
92+
7 3 5 6 1
93+
3 2 4 5
94+
Output 1:
95+
The Maximum length of repeated subarray is : 1
96+
97+
*/
98+

0 commit comments

Comments
 (0)