-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecial_Interger.cpp
More file actions
50 lines (42 loc) · 901 Bytes
/
Special_Interger.cpp
File metadata and controls
50 lines (42 loc) · 901 Bytes
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
/*
Given an array of integers A and an integer B, find and return the maximum value K such that there is no subarray in A of size K with sum of elements greater than B.
*/
#define ll long long int
bool isValid(vector<int> &arr, int target, int hops)
{
ll sum = 0;
int i = 0, j = 0;
for (; i < hops; ++i)
{
sum += arr[i];
}
for (; i < arr.size(); ++i)
{
if (sum > target)
return false;
sum += arr[i] - arr[j++];
}
if (sum > target)
return false;
return true;
}
int Solution::solve(vector<int> &A, int B)
{
int n = A.size();
int s = 0, e = n;
int ans = 0;
while (s <= e)
{
int mid = (s + (e - s) / 2);
if (isValid(A, B, mid))
{
s = mid + 1;
ans = mid;
}
else
{
e = mid - 1;
}
}
return ans;
}