-
Notifications
You must be signed in to change notification settings - Fork 617
/
Book_Allocation.java
108 lines (94 loc) · 3.42 KB
/
Book_Allocation.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Book Allocation
Provided "n" number of books and "m" number of students. Books are arranged in increasing number of its pages. Each student is assigned to read pages in consecutive order.
Assign books in such a manner that maximum number of pages each student get to read is minimum.
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Book_Allocation {
// function to check if "key" number of pages can be allocated or not
public static boolean pageread(int a[], int n, int key, int m) {
int sum = 0;
int cnt = 1;
for (int j = 0; j < n; j++) {
// if number of pages is greater than key then it can't be allocated
if (a[j] > key)
return false;
// counting the number of students required to allocate "sum" number of pages
if (sum + a[j] > key) {
// incrementing the number students and updating current number of pages in "sum"
cnt++;
sum = a[j];
// if count of students is greater than given students "m", then its not
// possible to allocate these many books to given students "m"
if (cnt > m)
return false;
}
// else update current number of pages
else {
sum += a[j];
}
}
return true;
}
// function to find the page distribution
public static int binary_search(int a[], int n, int m) {
int ans = Integer.MAX_VALUE;
// if students are greater than number of books then its impossible to allocate pages
if (n < m)
return -1;
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
int low = 0;
int high = sum;
while (low <= high) {
int mid = (low + high) / 2;
// checking if "mid" number of pages can be distributed as minimum possible pages
if (pageread(a, n, mid, m) == true) {
// if "mid" number of pages can be allocated then find minimum of current
// minimum pages and "mid"
ans = Math.min(ans, mid);
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of pages of each books: ");
int n = sc.nextInt();
System.out.print("Enter number of students: ");
int m = sc.nextInt();
// array for page distribution of each book
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int ans = binary_search(a, n, m);
System.out.println("Minimum number of pages = " + ans);
}
}
/*
Test Cases
1.
Input -
Enter number of pages of each books: 6
Enter number of students: 3
13 28 34 40 57 85
Output -
Minimum number of pages = 97
2.
Input -
Enter number of pages of each books: 7
Enter number of students: 5
10 24 33 49 59 66 107
Output -
Minimum number of pages = 107
Time complexity : O(n*log(sum)) where n = number of books, sum = sum of the pages of all the books
Space complexity: O(1), no extra space is used
*/