3151. Special Array I
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Advertisement
Intuition
Check if array is "special": every adjacent pair has different parity (one odd, one even).
Algorithm
- 1For each i from 0 to n-2: if nums[i]%2 == nums[i+1]%2: return false.
Common Pitfalls
- •Adjacent elements must alternate parity. One same-parity pair fails the check.
3151.cs
C#
// Approach: Check every adjacent pair has different parity.
// Time: O(n) Space: O(1)
public class Solution
{
public bool IsArraySpecial(int[] nums)
{
for (int i = 1; i < nums.Length; ++i)
{
if (nums[i] % 2 == nums[i - 1] % 2)
return false;
}
return true;
}
}Advertisement
Was this solution helpful?