Back to problems

Longest balanced subarray

Algorithm · Meta · Medium

You are given an integer array nums whose elements are restricted to the values 0 and 1. Among all contiguous subarrays (blocks of adjacent positions), find one that holds the same count of 0s as it does 1s, and report the largest possible length of such a block. Return the length as an integer. If no contiguous block can ever balance the two values — for instance when the entire array consists of a single value — the answer is 0. Example 1: Explanation: The block [0,1]…

Checking your access…