Skip to content

Commit 2350f02

Browse files
authored
Merge pull request #257 from ben1009/3151
feat: 3151
2 parents 31750b4 + 25a693e commit 2350f02

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/problem/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub mod p2938_separate_black_and_white_balls;
7272
pub mod p2956_find_common_elements_between_two_arrays;
7373
pub mod p2974_minimum_number_game;
7474
pub mod p3033_modify_the_matrix;
75+
pub mod p3151_special_array_i;
7576
pub mod p3153_sum_of_digit_differences_of_all_pairs;
7677
pub mod p3174_clear_digits;
7778
pub mod p3226_number_of_bit_changes_to_make_two_integers_equal;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// An array is considered special if every pair of its adjacent elements contains two numbers with
2+
// different parity.
3+
4+
// You are given an array of integers nums. Return true if nums is a special array, otherwise,
5+
// 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.
26+
// So the answer is true.
27+
28+
// Example 3:
29+
30+
// Input: nums = [4,3,1,6]
31+
32+
// Output: false
33+
34+
// Explanation:
35+
36+
// nums[1] and nums[2] are both odd. So the answer is false.
37+
38+
// Constraints:
39+
40+
// 1 <= nums.length <= 100
41+
// 1 <= nums[i] <= 100
42+
43+
// https://leetcode.com/problems/special-array-with-x-elements-greater-than-x
44+
// https://leetcode.com/problems/special-array-with-x-elements-greater-than-x/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
pub struct Solution {}
47+
48+
impl Solution {
49+
pub fn is_array_special(nums: Vec<i32>) -> bool {
50+
if nums.len() == 1 {
51+
return true;
52+
}
53+
54+
for i in 1..nums.len() {
55+
if nums[i] % 2 == nums[i - 1] % 2 {
56+
return false;
57+
}
58+
}
59+
60+
true
61+
}
62+
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn test_3151() {
70+
assert!(Solution::is_array_special(vec![1]));
71+
assert!(Solution::is_array_special(vec![2, 1, 4]));
72+
assert!(!Solution::is_array_special(vec![4, 3, 1, 6]));
73+
}
74+
}

0 commit comments

Comments
 (0)