`{"id":"7ede8736-1833-4367-beba-bdf0834c40a8","name":"Maximum Length Of Repeated Subarray","description":"1. You are given two arrays of integers arr1 and arr2.\r\n2. You have to find the maximum length of subarray that appears in both the given arrays.","inputFormat":"A number N \r\na1\r\na2.. N numbers\r\nA number M\r\nb1\r\nb2.. M numbers ","outputFormat":"An integer representing the maximum length of repeated subarray.","constraints":"1 &lt;= N,M &lt;= 10^4\r\n0 &lt;= a[i], b[i] &lt;= 10^3","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr1, int[] arr2) {\r\n // write your code here\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr1 = new int[n];\r\n for (int i = 0 ; i < n; i++) {\r\n arr1[i] = scn.nextInt();\r\n }\r\n\r\n int m = scn.nextInt();\r\n int[] arr2 = new int[m];\r\n for (int i = 0 ; i < m; i++) {\r\n arr2[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr1, arr2));\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5 4 3 2 1\r\n6\r\n7 8 4 3 2 5","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"a0616ee1-bed0-46fb-ae8c-ccf3ffa2d5fd","name":"Maximum Length Of Repeated Subarray","slug":"maximum-length-of-repeated-subarray","type":1}],"next":{"id":"09162248-4b5b-49d2-8a5a-ddc135c5b8f6","name":"Arithmetic Slices-2","type":3,"slug":"arithmetic-slices-2"},"prev":{"id":"2c1cce5a-4b4c-4f06-8a6e-371e12a9b619","name":"Maximum Length of Repeated Subarray MCQ","type":0,"slug":"maximum-length-of-repeated-subarray-mcq"}}}`

# Maximum Length Of Repeated Subarray

1. You are given two arrays of integers arr1 and arr2. 2. You have to find the maximum length of subarray that appears in both the given arrays.

`{"id":"7ede8736-1833-4367-beba-bdf0834c40a8","name":"Maximum Length Of Repeated Subarray","description":"1. You are given two arrays of integers arr1 and arr2.\r\n2. You have to find the maximum length of subarray that appears in both the given arrays.","inputFormat":"A number N \r\na1\r\na2.. N numbers\r\nA number M\r\nb1\r\nb2.. M numbers ","outputFormat":"An integer representing the maximum length of repeated subarray.","constraints":"1 &lt;= N,M &lt;= 10^4\r\n0 &lt;= a[i], b[i] &lt;= 10^3","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr1, int[] arr2) {\r\n // write your code here\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr1 = new int[n];\r\n for (int i = 0 ; i < n; i++) {\r\n arr1[i] = scn.nextInt();\r\n }\r\n\r\n int m = scn.nextInt();\r\n int[] arr2 = new int[m];\r\n for (int i = 0 ; i < m; i++) {\r\n arr2[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr1, arr2));\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5\r\n5 4 3 2 1\r\n6\r\n7 8 4 3 2 5","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"5539a6e8-c8bf-4f04-805c-e43e9d20e72a","name":"Dynamic Programming For Intermediate","slug":"dynamic-programming-for-intermediate-408","type":0},{"id":"a0616ee1-bed0-46fb-ae8c-ccf3ffa2d5fd","name":"Maximum Length Of Repeated Subarray","slug":"maximum-length-of-repeated-subarray","type":1}],"next":{"id":"09162248-4b5b-49d2-8a5a-ddc135c5b8f6","name":"Arithmetic Slices-2","type":3,"slug":"arithmetic-slices-2"},"prev":{"id":"2c1cce5a-4b4c-4f06-8a6e-371e12a9b619","name":"Maximum Length of Repeated Subarray MCQ","type":0,"slug":"maximum-length-of-repeated-subarray-mcq"}}}` Editor

# Maximum Length Of Repeated Subarray

medium

1. You are given two arrays of integers arr1 and arr2. 2. You have to find the maximum length of subarray that appears in both the given arrays.

## Constraints

1 <= N,M <= 10^4 0 <= a[i], b[i] <= 10^3

## Format

### Input

A number N a1 a2.. N numbers A number M b1 b2.. M numbers

### Output

An integer representing the maximum length of repeated subarray.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 5 4 3 2 1 6 7 8 4 3 2 5```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 ```

Discussions

Show Discussion

Related Resources 