forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook Allocation Problem.java
More file actions
103 lines (90 loc) · 3.64 KB
/
Book Allocation Problem.java
File metadata and controls
103 lines (90 loc) · 3.64 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
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
/*
* Book Allocation Problem (Binary Search on Answer)
*
* Description:
* You are given an array of integers 'pages' where pages[i] represents the number of pages in the i-th book
* and an integer 'students' representing the number of students.
* The goal is to allocate books to each student such that:
* - Each student gets at least one book
* - Each book is assigned to exactly one student
* - The maximum number of pages assigned to a student is minimized
*
* Approach:
* - This is solved using Binary Search on Answer (the answer lies between max(pages) and sum(pages)).
* - Check function: given a maximum pages limit, can we allocate books so that no student gets more than this?
* - Use binary search to find the minimum possible value of this maximum.
*
* Time Complexity: O(n * log(sum of pages)), where n is the number of books
* Space Complexity: O(1)
*/
import java.util.*;
class Solution {
// Check if it's possible to allocate books with maxPages as the upper limit
private boolean isPossible(int[] pages, int students, int maxPages) {
int countStudents = 1;
int currentSum = 0;
for (int p : pages) {
if (p > maxPages) return false; // single book > maxPages → not possible
if (currentSum + p > maxPages) {
countStudents++;
currentSum = p;
if (countStudents > students) return false;
} else {
currentSum += p;
}
}
return true;
}
public int allocateBooks(int[] pages, int students) {
if (students > pages.length) return -1; // not enough books
int low = Arrays.stream(pages).max().getAsInt();
int high = Arrays.stream(pages).sum();
int result = high;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isPossible(pages, students, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
}
public class BookAllocation {
public static void main(String[] args) {
Solution sol = new Solution();
// Test Case 1
System.out.println("Test Case 1:");
int[] pages1 = {12, 34, 67, 90};
int students1 = 2;
System.out.println("Books: " + Arrays.toString(pages1));
System.out.println("Students: " + students1);
System.out.println("Minimum Maximum Pages: " + sol.allocateBooks(pages1, students1));
System.out.println();
// Test Case 2
System.out.println("Test Case 2:");
int[] pages2 = {10, 20, 30, 40};
int students2 = 2;
System.out.println("Books: " + Arrays.toString(pages2));
System.out.println("Students: " + students2);
System.out.println("Minimum Maximum Pages: " + sol.allocateBooks(pages2, students2));
System.out.println();
// Test Case 3
System.out.println("Test Case 3:");
int[] pages3 = {5, 10, 30, 20, 15};
int students3 = 3;
System.out.println("Books: " + Arrays.toString(pages3));
System.out.println("Students: " + students3);
System.out.println("Minimum Maximum Pages: " + sol.allocateBooks(pages3, students3));
System.out.println();
// Test Case 4 (Edge Case: students > books)
System.out.println("Test Case 4:");
int[] pages4 = {10, 20, 30};
int students4 = 4;
System.out.println("Books: " + Arrays.toString(pages4));
System.out.println("Students: " + students4);
System.out.println("Minimum Maximum Pages: " + sol.allocateBooks(pages4, students4));
}
}