`{"id":"209687a1-3f9a-455f-a453-ae4ebf6cdeb3","name":"Longest Subarray With Equal Number Of Zeroes And Ones","description":"1. You are given an array that contains only 0s and 1s.\r\n2. You have to find length of the longest subarray with equal number of 0s and 1s.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number representing length of the longest subarray with equal number of 0s and 1s.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 1","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> arr) {\n // write your code here\n}\n\nint main() {\n int n;\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);\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 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":"6\r\n0 1 1 0 1 1","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/1WugaISSWx8?end=12","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":"4b81ba2f-2595-4b5b-9f67-2d01ae1d997b","name":"Longest Subarray With Equal Number Of Zeroes And Ones","slug":"longest-subarray-with-equal-number-of-zeroes-and-ones","type":1}],"next":{"id":"08b493f7-54d4-41bd-95e9-533747d743bc","name":"Longest Subarray With Equal Number Of Zeroes And Ones MCQ","type":0,"slug":"longest-subarray-with-equal-number-of-zeroes-and-ones-mcq"},"prev":{"id":"b80ffd06-464a-4384-a247-231247c03bd1","name":"Count Of Subarrays With Sum Divisible By K","type":3,"slug":"count-of-subarrays-with-sum-divisible-by-k"}}}`

# Longest Subarray With Equal Number Of Zeroes And Ones

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

`{"id":"209687a1-3f9a-455f-a453-ae4ebf6cdeb3","name":"Longest Subarray With Equal Number Of Zeroes And Ones","description":"1. You are given an array that contains only 0s and 1s.\r\n2. You have to find length of the longest subarray with equal number of 0s and 1s.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number representing length of the longest subarray with equal number of 0s and 1s.","constraints":"1 &lt;= N &lt;= 10^5\r\n0 &lt;= arr[i] &lt;= 1","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> arr) {\n // write your code here\n}\n\nint main() {\n int n;\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);\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 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":"6\r\n0 1 1 0 1 1","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/1WugaISSWx8?end=12","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":"4b81ba2f-2595-4b5b-9f67-2d01ae1d997b","name":"Longest Subarray With Equal Number Of Zeroes And Ones","slug":"longest-subarray-with-equal-number-of-zeroes-and-ones","type":1}],"next":{"id":"08b493f7-54d4-41bd-95e9-533747d743bc","name":"Longest Subarray With Equal Number Of Zeroes And Ones MCQ","type":0,"slug":"longest-subarray-with-equal-number-of-zeroes-and-ones-mcq"},"prev":{"id":"b80ffd06-464a-4384-a247-231247c03bd1","name":"Count Of Subarrays With Sum Divisible By K","type":3,"slug":"count-of-subarrays-with-sum-divisible-by-k"}}}`

Editor

# Longest Subarray With Equal Number Of Zeroes And Ones

easy

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

## Constraints

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

## Format

### Input

A number N arr1 arr2.. N numbers

### Output

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

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

### 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;}4 ```

Question Video

Discussions

Show Discussion

Related Resources