Skip to content

Commit 35467b2

Browse files
committed
Added solution of problem 1608
1 parent 4ddb821 commit 35467b2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ pub mod s1588_sum_of_all_odd_length_subarrays;
343343
pub mod s1592_rearrange_spaces_between_words;
344344
pub mod s1598_crawler_log_folder;
345345
pub mod s1603_design_parking_system;
346+
pub mod s1608_special_array_with_x_elements_greater_than_or_equal_x;
346347
pub mod s1619_mean_of_array_after_removing_some_elements;
347348
pub mod s1935_maximum_number_of_words_you_can_type;
348349
pub mod s1957_delete_characters_to_make_fancy_string;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pub fn special_array(nums: Vec<i32>) -> i32 {
2+
let arr_count = nums.iter().fold([0; 1001], |mut acc, &x| {
3+
acc[x as usize] += 1;
4+
acc
5+
});
6+
let mut prev_sum = 0;
7+
let l = nums.len() as i32;
8+
9+
for i in 0..=l {
10+
let after_sum = l - prev_sum;
11+
prev_sum += arr_count[i as usize];
12+
13+
if after_sum == i {
14+
return i;
15+
} else if prev_sum == l {
16+
break;
17+
}
18+
}
19+
20+
-1
21+
}
22+
23+
#[cfg(test)]
24+
mod test {
25+
use super::*;
26+
27+
#[test]
28+
fn test_case_1() {
29+
assert_eq!(special_array(vec![3, 5]), 2);
30+
}
31+
32+
#[test]
33+
fn test_case_2() {
34+
assert_eq!(special_array(vec![0, 0]), -1);
35+
}
36+
37+
#[test]
38+
fn test_case_3() {
39+
assert_eq!(special_array(vec![0, 4, 3, 0, 4]), 3);
40+
}
41+
}

0 commit comments

Comments
 (0)