-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage_Allocation.cpp
More file actions
71 lines (49 loc) · 805 Bytes
/
Page_Allocation.cpp
File metadata and controls
71 lines (49 loc) · 805 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <climits>
using namespace std;
bool isPosible(int arr[],int n,int m,int min){
int studentreq=1,sum=0;
for(int i=0;i<n;i++){
if(arr[i] > min){
return false;
}
if(sum+arr[i]>min){
studentreq++;
sum=arr[i];
if(studentreq>m){
return false;
}
}else{
sum+=arr[i];
}
}
return true;
}
int AllocationMinimum(int arr[],int n,int m){
int sum=0;
if(n<m){
return -1;
}
for (int i = 0; i <n; i++)
{
sum+=sum+arr[i];
}
int start=0,end=sum,ans=INT_MAX;
while(start<=end){
int mid=(start+end)/2;
if(isPosible(arr,n,m,mid)){
ans=min(ans,mid);
end=mid-1;
}else{
start=mid+1;
}
}
return ans;
}
int main(){
int arr[]={12,34,67,90};
int n=4;
int m=2;
cout<<AllocationMinimum(arr,n,m)<<endl;
return 0;
}