-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookAllocation.cpp
More file actions
50 lines (41 loc) · 1.08 KB
/
Copy pathbookAllocation.cpp
File metadata and controls
50 lines (41 loc) · 1.08 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
47
48
49
50
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isValid(int arr, int n, int mid, int m){
int studentCount = 1;
int currentSum = 0,i;
for(i=0; i<n; i++){
if(arr[i] > mid) return false; // If a single book is greater than mid, it's not possible
if(currentSum + arr[i] > mid){
studentCount++;
currentSum = arr[i];
}
if(studentCount > m) {
return false;
} else {
return true;
}
}
}
int main(){
int s=0,en,n,m;
cout<<"Enter the number of students: ";
cin>> m; // n is the number of books, m is the number of students;
int arr[4]= {12, 34, 67, 90}; // Example book sizes
n = sizeof(arr) / sizeof(arr[0]); // Calculate number of books
int sum=0;
for(int i=0;i<n;i++){
cin>> arr[i];
sum += arr[i];
}
en = sum;
while(s <= en){
int mid = (s + en) / 2;
if(isValid(arr[4],n, mid,m)){
en = mid - 1;
} else {
s = mid + 1;
}
}
}