Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Largest subarray of 0's and 1's

Easy Acceptance 50.00% Points 20.00

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.

Hints
Hint 1
NA
Constraints
  • 1 <= arr.size() <= 10^5
  • 0 <= arr[i] <= 1
Companies
Amazon Morgan Stanly Paytm
Topics
Data Structures Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions