#K88092. Longest Common Subarray
Longest Common Subarray
Longest Common Subarray
You are given two integer arrays arr1
and arr2
of size N. Your task is to find the length of the longest contiguous subarray (subarray means a consecutive segment) that appears in both arrays.
Formally, if you denote the subarrays as segments of the arrays, you need to compute the maximum k such that there exists indices i and j where arr1[i], arr1[i+1], ..., arr1[i+k-1]
is exactly equal to arr2[j], arr2[j+1], ..., arr2[j+k-1]
. The answer should be output as a single integer.
Note: Both arrays have the same number of elements N.
inputFormat
The input is given from standard input (stdin) and is formatted as follows:
- The first line contains an integer N denoting the size of the arrays.
- The second line contains N space-separated integers, representing the elements of the first array
arr1
. - The third line contains N space-separated integers, representing the elements of the second array
arr2
.
outputFormat
Output a single integer on standard output (stdout) representing the length of the longest contiguous subarray that is common to both arrays.
## sample5
1 2 3 2 1
3 2 1 4 7
3