-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (52 loc) · 1.4 KB
/
main.go
File metadata and controls
56 lines (52 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source: https://leetcode.com/problems/special-array-i
// Title: Special Array I
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func isArraySpecial(nums []int) bool {
n := len(nums)
for i := range n - 1 {
if nums[i]%2 == nums[i+1]%2 {
return false
}
}
return true
}