-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMake_Sum_Divisible_by_P.cpp
48 lines (42 loc) · 1.5 KB
/
Make_Sum_Divisible_by_P.cpp
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
PROBLEM STATEMENT :- 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.
Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
A subarray is defined as a contiguous block of elements in the array.
Example 1:-
Input: nums = [3,1,4,2], p = 6
Output: 1
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.
SOLUTION :-
class Solution {
public:
int minSubarray(vector<int>& nums, int p)
{
int n = nums.size();
long long int mod[n], total = 0;
for(int i = 0; i<n; i++)
{
mod[i] = (nums[i] + p) % p;
total += nums[i];
}
if(total%p==0)
return 0;
long long int target_rem = total%p;
int res = INT_MAX;
unordered_map<long long int, int> mp;
mp[0] = -1;
long long int curr = 0;
for(int i=0; i<n; i++)
{
curr = (curr + nums[i] + p)%p;
mp[curr] = i;
long long int mod = (curr - target_rem + p)%p;
if(mp.find(mod)!= mp.end())
res = min(res, i-mp[mod]);
}
if(res == INT_MAX || res>=n)
return -1;
else
return res;
}
};