8000 Add 3151_special_array_one · m3xw3ll/LeetCode@2ecb0d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ecb0d5

Browse files
committed
Add 3151_special_array_one
1 parent 1904fc5 commit 2ecb0d5

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Algorithms/3151_special_array_one.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Special Array I](https://leetcode.com/problems/special-array-i/description/)
2+
3+
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.
4+
5+
You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
6+
7+
Example 1:
8+
```
9+
Input: nums = [1]
10+
11+
Output: true
12+
13+
Explanation:
14+
15+
There is only one element. So the answer is true.
16+
```
17+
Example 2:
18+
```
19+
Input: nums = [2,1,4]
20+
21+
Output: true
22+
23+
Explanation:
24+
25+
There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.
26+
```
27+
Example 3:
28+
```
29+
Input: nums = [4,3,1,6]
30+
31+
Output: false
32+
33+
Explanation:
34+
35+
nums[1] and nums[2] are both odd. So the answer is false.
36+
```
37+
Solution
38+
```python
39+
class Solution:
40+
def isArraySpecial(self, nums: List[int]) -> bool:
41+
for i in range(len(nums) - 1):
42+
if nums[i] % 2 == nums[i+1] % 2:
43+
return False
44+
return True
45+
```
46+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def is_array_special(nums):
2+
for i in range(len(nums) - 1):
3+
if nums[i] % 2 == nums[i+1] % 2:
4+
return False
5+
return True
6+
7+
8+
print(is_array_special([4,3,1,6]))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
757757
| 3099 | [Harshad Number](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3099_harshad_number.md)
758758
| 3110 | [Score of a String](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3110_score_of_a_string.md)
759759
| 3131 | [Find the Integer Added to Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3131_find_the_integer_added_to_array_one.md)
760+
| 3151 | [Special Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3151_special_array_one.md)
760761
| 3162 | [Find the Number of Good Pairs I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3162_find_the_number_of_good_pairs_one.md)
761762
| 3174 | [Clear Digits](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3174_clear_digits.md)
762763
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3190_find_minimum_operations_to_make_all_elements_divisible_by_three.md)

0 commit comments

Comments
 (0)
0