Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a binary array nums.
We call a subarray alternating if no two adjacent elements in the subarray have the same value.
Return the number of alternating subarrays in nums.
Example 1:
Input: nums = [0,1,1,1]
Output: 5
Explanation:
The following subarrays are alternating: [0], [1], [1], [1], and [0,1].
Example 2:
Input: nums = [1,0,1,0]
Output: 10
Explanation:
Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.Problem summary: You are given a binary array nums. We call a subarray alternating if no two adjacent elements in the subarray have the same value. Return the number of alternating subarrays in nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[0,1,1,1]
[1,0,1,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3101: Count Alternating Subarrays
class Solution {
public long countAlternatingSubarrays(int[] nums) {
long ans = 1, s = 1;
for (int i = 1; i < nums.length; ++i) {
s = nums[i] != nums[i - 1] ? s + 1 : 1;
ans += s;
}
return ans;
}
}
// Accepted solution for LeetCode #3101: Count Alternating Subarrays
func countAlternatingSubarrays(nums []int) int64 {
ans, s := int64(1), int64(1)
for i, x := range nums[1:] {
if x != nums[i] {
s++
} else {
s = 1
}
ans += s
}
return ans
}
# Accepted solution for LeetCode #3101: Count Alternating Subarrays
class Solution:
def countAlternatingSubarrays(self, nums: List[int]) -> int:
ans = s = 1
for a, b in pairwise(nums):
s = s + 1 if a != b else 1
ans += s
return ans
// Accepted solution for LeetCode #3101: Count Alternating Subarrays
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3101: Count Alternating Subarrays
// class Solution {
// public long countAlternatingSubarrays(int[] nums) {
// long ans = 1, s = 1;
// for (int i = 1; i < nums.length; ++i) {
// s = nums[i] != nums[i - 1] ? s + 1 : 1;
// ans += s;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3101: Count Alternating Subarrays
function countAlternatingSubarrays(nums: number[]): number {
let [ans, s] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
s = nums[i] !== nums[i - 1] ? s + 1 : 1;
ans += s;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.