-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryBook.cpp
44 lines (42 loc) · 864 Bytes
/
binaryBook.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
#include <iostream>
using namespace std;
int main()
{
int arr[100];
int n;
cout << "Enter size of the array" << endl;
cin >> n;
cout << "Enter elements in the array" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int sum = 0, max = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i] < arr[i + 1])
{
max = arr[i + 1];
}
sum = sum + arr[i];
}
int start = max,end=sum,mid,ans,m;
while(start<=end){
mid=(start+end)/2;
int page=0,count=1;
for(int i=0;i<n;i++){
page=page+arr[i];
if(page>mid){
count+=1;
page=arr[i];
}
}
if(count<=m){
ans=mid;
end=mid-1;
}
else{
start=mid+1;
}
}
}