forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-size-subarray-in-infinite-array.cpp
58 lines (47 loc) · 1.95 KB
/
minimum-size-subarray-in-infinite-array.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
49
50
51
52
53
54
55
56
57
58
// https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/
/*
Approach:
The problem is similar to 'Find minimum length circular subarray such that it's sum ==
target.', but with a slight twist.
Here we are given to consider array of infinite length, (concatenate the array multiple
times). Let's say sum of all the numbers in a single array = S.
So if we think about it:
if target <= S, we have to consider only one instance of the array
but if target > S:
Here, we have to consider multiple instances. We can do it in the following way:
(sum of some last elements) + k*(sum of the array) + (sum of some front elements)
The (sum of some front elements) + (sum of some last elements) is = to target%S and
k = (target/S) * n
minimum length circular subarray such that it's sum == target%S can be calculated by
using the sliding window technique.
*/
class Solution {
public:
int minSizeSubarray(vector<int>& nums, int target) {
int n = nums.size();
// Calculate the sum of all elements
long long sum = accumulate(nums.begin(),nums.end(),0ll);
// If target > sum, then we have to consider multiple copies of the array, hence we
// initialize the ans to number of elements we have to definitely consider and rest we
// calculate.
int ans = (target/sum) * n;
target%=sum;
// Finding minimum length circular subarray such that sum of it == target.
// Using sliding window technique for the calculation.
int mn = INT_MAX, st = 0, ed = 0;
long long tempSum = 0;
for(; ed<2*n; ed++){
tempSum += nums[ed%n];
while(tempSum>target){
tempSum -= nums[st%n];
st++;
}
if(tempSum == target){
mn = min(mn, ed-st+1);
}
}
if (mn == INT_MAX) return -1;
ans+=mn;
return ans;
}
};