forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.01.2024.cpp
48 lines (42 loc) · 998 Bytes
/
07.01.2024.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
class Solution {
public:
bool check(int arr[], int N, int K, int mid){
int sum=0;
int count=0;
for(int i=0;i<N;i++){
if(arr[i]>mid){
return false;
}
sum+=arr[i];
if(sum > mid){
count++;
sum=arr[i];
}
}
count++;
if(count > K){
return false;
}
return true;
}
int splitArray(int arr[] ,int N, int K) {
// code here
int mini=0, maxi=0;
for(int i=0 ;i<N; i++){
mini = max(mini, arr[i]);
maxi +=arr[i];
}
int ans=maxi;
while(mini <= maxi){
int mid = (mini+maxi)/2;
if(check(arr, N, K, mid)){
ans=mid;
maxi=mid-1;
}
else{
mini=mid+1;
}
}
return ans;
}
};