Talentd
FleetCode

3151. Special Array I

Easy81.6% acceptance6 companies

An array is considered special if every pair of its adjacent elements contains two numbers with different parity.

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 <= 100
  • 1 <= nums[i] <= 100

Topics

Hints open one at a time

Hint 1
Try to check the parity of each element and its previous element.
16:06
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def isArraySpecial(self, nums: List[int]) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
[1]
Input
[2,1,4]
Input
[4,3,1,6]
Your code is saved on this device.Solution