`{"id":"f9a407b4-d87c-4200-bdc4-ee2d656a4023","name":"Longest Subarray With Equal Number Of 0s 1s And 2s","description":"1. You are given an array that contains only 0s, 1s, and 2s.\r\n2. You have to find length of the longest subarray with equal number of 0s, 1s, and 2s.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number representing length of the longest subarray with equal number of 0s, 1s, and 2s.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 2","sampleCode":{"cpp":{"code":"#include \"bits/stdc++.h\"\nusing namespace std;\n\nint solution(vector<int> &v){\n //Write your code here\n }\n\n\nint main(){\n int n;cin>> n;\n vector<int> arr(n);\n for(int i=0;i<n;i++){\n cin >> arr[i];\n }\n cout << solution(arr) << endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr) {\r\n // write your code here\r\n\r\n return 0;\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[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr));\r\n }\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n0 1 0 2 0 1 0","sampleOutput":"3\r\n","questionVideo":"https://www.youtube.com/embed/MRoWBJvJeLQ?end=18","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":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"86b01ea6-97a2-469c-a595-64c20f28b01a","name":"Longest Subarray With Equal Number Of 0s 1s And 2s","slug":"longest-subarray-with-equal-number-of-0s-1s-and-2s","type":1}],"next":{"id":"11d8b3a0-8fcd-4a8d-862d-424642e0934b","name":"Longest Subarray With Equal Number Of 0s 1s And 2s MCQ","type":0,"slug":"longest-subarray-with-equal-number-of-0s-1s-and-2s-mcq"},"prev":{"id":"2be139b6-e155-4136-bd83-52e69154e7dc","name":"Count Of Subarrays with Equal Number of Zeroes And Ones","type":3,"slug":"count-of-subarrays-with-equal-number-of-zeroes-and-ones"}}}`

# Longest Subarray With Equal Number Of 0s 1s And 2s

1. You are given an array that contains only 0s, 1s, and 2s. 2. You have to find length of the longest subarray with equal number of 0s, 1s, and 2s.

`{"id":"f9a407b4-d87c-4200-bdc4-ee2d656a4023","name":"Longest Subarray With Equal Number Of 0s 1s And 2s","description":"1. You are given an array that contains only 0s, 1s, and 2s.\r\n2. You have to find length of the longest subarray with equal number of 0s, 1s, and 2s.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number representing length of the longest subarray with equal number of 0s, 1s, and 2s.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 2","sampleCode":{"cpp":{"code":"#include \"bits/stdc++.h\"\nusing namespace std;\n\nint solution(vector<int> &v){\n //Write your code here\n }\n\n\nint main(){\n int n;cin>> n;\n vector<int> arr(n);\n for(int i=0;i<n;i++){\n cin >> arr[i];\n }\n cout << solution(arr) << endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr) {\r\n // write your code here\r\n\r\n return 0;\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[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr));\r\n }\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n0 1 0 2 0 1 0","sampleOutput":"3\r\n","questionVideo":"https://www.youtube.com/embed/MRoWBJvJeLQ?end=18","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":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"86b01ea6-97a2-469c-a595-64c20f28b01a","name":"Longest Subarray With Equal Number Of 0s 1s And 2s","slug":"longest-subarray-with-equal-number-of-0s-1s-and-2s","type":1}],"next":{"id":"11d8b3a0-8fcd-4a8d-862d-424642e0934b","name":"Longest Subarray With Equal Number Of 0s 1s And 2s MCQ","type":0,"slug":"longest-subarray-with-equal-number-of-0s-1s-and-2s-mcq"},"prev":{"id":"2be139b6-e155-4136-bd83-52e69154e7dc","name":"Count Of Subarrays with Equal Number of Zeroes And Ones","type":3,"slug":"count-of-subarrays-with-equal-number-of-zeroes-and-ones"}}}` Editor

# Longest Subarray With Equal Number Of 0s 1s And 2s

easy

1. You are given an array that contains only 0s, 1s, and 2s. 2. You have to find length of the longest subarray with equal number of 0s, 1s, and 2s.

## Constraints

1 <= N <= 10^5 0 <= arr[i] <= 2

## Format

### Input

A number N arr1 arr2.. N numbers

### Output

A number representing length of the longest subarray with equal number of 0s, 1s, and 2s.

## 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;}7 0 1 0 2 0 1 0```

### 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 ```

Question Video

Discussions

Show Discussion

Related Resources 