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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
An array is considered special if the parity of every pair of adjacent elements is different. In other words, one element in each pair must be even, and the other must be odd.
You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
Example 1:
Input: nums = [1]
Output: true
Explanation:
There is only one element. So the answer is true.
Example 2:
Input: nums = [2,1,4]
Output: true
Explanation:
There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.
Example 3:
Input: nums = [4,3,1,6]
Output: false
Explanation:
nums[1] and nums[2] are both odd. So the answer is false.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: An array is considered special if the parity of every pair of adjacent elements is different. In other words, one element in each pair must be even, and the other must be odd. You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1]
[2,1,4]
[4,3,1,6]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3151: Special Array I
class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #3151: Special Array I
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
# Accepted solution for LeetCode #3151: Special Array I
class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
// Accepted solution for LeetCode #3151: Special Array I
// 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 #3151: Special Array I
// class Solution {
// public boolean isArraySpecial(int[] nums) {
// for (int i = 1; i < nums.length; ++i) {
// if (nums[i] % 2 == nums[i - 1] % 2) {
// return false;
// }
// }
// return true;
// }
// }
// Accepted solution for LeetCode #3151: Special Array I
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
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.