Skip to content

Commit 297f988

Browse files
committed
1590. Make Sum Divisible by P: AC
1 parent 4b8ad97 commit 297f988

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1198,3 +1198,4 @@ mod s1584_min_cost_to_connect_all_points;
11981198
mod s1585_check_if_string_is_transformable_with_substring_sort_operations;
11991199
mod s1588_sum_of_all_odd_length_subarrays;
12001200
mod s1589_maximum_sum_obtained_of_any_permutation;
1201+
mod s1590_make_sum_divisible_by_p;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* [1590] Make Sum Divisible by P
3+
*
4+
* Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.
5+
* Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
6+
* A subarray is defined as a contiguous block of elements in the array.
7+
*
8+
* Example 1:
9+
*
10+
* Input: nums = [3,1,4,2], p = 6
11+
* Output: 1
12+
* Explanation: The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
13+
*
14+
* Example 2:
15+
*
16+
* Input: nums = [6,3,5,2], p = 9
17+
* Output: 2
18+
* Explanation: We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.
19+
*
20+
* Example 3:
21+
*
22+
* Input: nums = [1,2,3], p = 3
23+
* Output: 0
24+
* Explanation: Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 1 <= nums.length <= 10^5
30+
* 1 <= nums[i] <= 10^9
31+
* 1 <= p <= 10^9
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// problem: https://leetcode.com/problems/make-sum-divisible-by-p/
37+
// discuss: https://leetcode.com/problems/make-sum-divisible-by-p/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
impl Solution {
42+
pub fn min_subarray(nums: Vec<i32>, p: i32) -> i32 {
43+
let nums = nums.into_iter().map(|x| x as i64).collect::<Vec<_>>();
44+
let p = p as i64;
45+
let sum = nums.iter().sum::<i64>();
46+
let rem = sum % p;
47+
if rem == 0 {
48+
return 0;
49+
}
50+
let mut result = nums.len() as i64;
51+
let mut m = std::collections::HashMap::new();
52+
let mut sum = 0;
53+
54+
for (i, &num) in nums.iter().enumerate() {
55+
let i = i as i64;
56+
sum += num;
57+
let k = sum % p;
58+
if k == rem {
59+
result = result.min(i + 1);
60+
}
61+
if let Some(&j) = m.get(&(k - rem)) {
62+
result = result.min(i - j);
63+
}
64+
if let Some(&j) = m.get(&(p + k - rem)) {
65+
result = result.min(i - j);
66+
}
67+
m.insert(k, i);
68+
}
69+
70+
if result >= nums.len() as i64 {
71+
return -1;
72+
}
73+
74+
result as _
75+
}
76+
}
77+
78+
// submission codes end
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn test_1590_example_1() {
86+
let nums = vec![3, 1, 4, 2];
87+
let p = 6;
88+
89+
let result = 1;
90+
91+
assert_eq!(Solution::min_subarray(nums, p), result);
92+
}
93+
94+
#[test]
95+
fn test_1590_example_2() {
96+
let nums = vec![6, 3, 5, 2];
97+
let p = 9;
98+
99+
let result = 2;
100+
101+
assert_eq!(Solution::min_subarray(nums, p), result);
102+
}
103+
104+
#[test]
105+
fn test_1590_example_3() {
106+
let nums = vec![1, 2, 3];
107+
let p = 3;
108+
109+
let result = 0;
110+
111+
assert_eq!(Solution::min_subarray(nums, p), result);
112+
}
113+
}

0 commit comments

Comments
 (0)