|
| 1 | +<h2><a href="https://leetcode.com/problems/longest-balanced-subarray-i">4045. Longest Balanced Subarray I</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>A <strong><span data-keyword="subarray-nonempty">subarray</span></strong> is called <strong>balanced</strong> if the number of <strong>distinct even</strong> numbers in the subarray is equal to the number of <strong>distinct odd</strong> numbers.</p> |
| 4 | + |
| 5 | +<p>Return the length of the <strong>longest</strong> balanced subarray.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,5,4,3]</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li>The longest balanced subarray is <code>[2, 5, 4, 3]</code>.</li> |
| 19 | + <li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[5, 3]</code>. Thus, the answer is 4.</li> |
| 20 | +</ul> |
| 21 | +</div> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<div class="example-block"> |
| 26 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,2,2,5,4]</span></p> |
| 27 | + |
| 28 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 29 | + |
| 30 | +<p><strong>Explanation:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li>The longest balanced subarray is <code>[3, 2, 2, 5, 4]</code>.</li> |
| 34 | + <li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[3, 5]</code>. Thus, the answer is 5.</li> |
| 35 | +</ul> |
| 36 | +</div> |
| 37 | + |
| 38 | +<p><strong class="example">Example 3:</strong></p> |
| 39 | + |
| 40 | +<div class="example-block"> |
| 41 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,2]</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 44 | + |
| 45 | +<p><strong>Explanation:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li>The longest balanced subarray is <code>[2, 3, 2]</code>.</li> |
| 49 | + <li>It has 1 distinct even number <code>[2]</code> and 1 distinct odd number <code>[3]</code>. Thus, the answer is 3.</li> |
| 50 | +</ul> |
| 51 | +</div> |
| 52 | + |
| 53 | +<p> </p> |
| 54 | +<p><strong>Constraints:</strong></p> |
| 55 | + |
| 56 | +<ul> |
| 57 | + <li><code>1 <= nums.length <= 1500</code></li> |
| 58 | + <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> |
| 59 | +</ul> |
0 commit comments