Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Largest subarray of 0's and 1's

Easy Acceptance 50% Points 20

Given an array arr of 0s and 1s. Find and return the length of the longest subarray with equal number of 0s and 1s.

Examples
Example 1

Example 1

Input: arr[] = [1, 0, 1, 1, 1, 0, 0]

Output: 6

Explanation: arr[1...6] is the longest subarray with three 0s and three 1s.

Example 2

Example 2

Input: arr[] = [0, 0, 1, 1, 0]

Output: 4 Explnation: arr[0...3] or arr[1...4] is the longest subarray with two 0s and two 1s.

Example 3

Example 3

Input: arr[] = [0]

Output: 0 Explnation: There is no subarray with an equal number of 0s and 1s.

Constraints
  • 1 <= arr.size() <= 10^5
  • 0 <= arr[i] <= 1
Topics
Data Structures Basics
Companies
Amazon Morgan Stanly Paytm
Added 18 Aug 2025 50% acceptance
Ln 1, Col 1Sign in to save your code
0 0 1 1 0
4