-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Division.cpp
More file actions
46 lines (45 loc) · 1.16 KB
/
Array_Division.cpp
File metadata and controls
46 lines (45 loc) · 1.16 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastio \
{ \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
}
int main()
{
fastio
ll n,
k, maxi = 0;
cin >> n >> k;
ll arr[n + 1];
for (ll i = 0; i < n; i++)
{
cin >> arr[i];
maxi = max(arr[i], maxi);
}
// cout << maxi << endl;
ll low = maxi, high = 1e18, ans;
while (low <= high)
{
ll mid = (low + high) / 2;
ll sum = 0, block = 1;
for (ll i = 0; i < n; i++)
{
if (sum + arr[i] > mid) // if i am finding the ans is 10 then i should stop when i crosing the range of 10.and i have to made a sub array (block++)and sttarting counting agaim
{
block++;
sum = 0;
}
sum += arr[i];
}
if (block <= k)
{
high = mid - 1;
ans = mid;
}
else
low = mid + 1; // if sub array is greater than k .then we should reduce sub array
}
cout << ans << endl;
}