3151. Special Array I
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Check if array is "special": every adjacent pair has different parity (one odd, one even).
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;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)