Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandeep-BlackHat authored Feb 6, 2021
1 parent 566ac8d commit b2f5134
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include<bits/stdc++.h>
using namespace std;


int findPages(int arr[], int n, int m);

int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int A[n];
for(int i=0;i<n;i++){
cin>>A[i];
}
int m;
cin>>m;
cout << findPages(A, n, m) << endl;
}
return 0;
}

bool solve(int *a, int n, int mid, int m){
int stu = 1;
int sum = 0;
for(int i=0; i<n; i++){
if(a[i]>mid) return false;
if(a[i]+sum > mid){
stu++;
sum = a[i];
if(stu>m) return false;
}
else sum += a[i];
}
return true;
}

int findPages(int *a, int n, int m) {
int lb = 0;
int s = 0;
int ans = 0;
for(int i=0; i<n; i++) s+= a[i];
int ub = s;
while(lb<=ub){
int mid = (lb + ub) / 2;
if(solve(a, n, mid, m)){
ans = mid;
ub = mid - 1;
}
else{
lb = mid + 1;
}
}
return ans;
}

0 comments on commit b2f5134

Please # to comment.