-
Notifications
You must be signed in to change notification settings - Fork 35
/
MinimumNumberOfPages.java
137 lines (112 loc) · 3.02 KB
/
MinimumNumberOfPages.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// { Driver Code Starts
//Initial Template for Java
/*package whatever //do not write package name here */
/*
You are given N number of books. Every ith book has Ai number of pages.
You have to allocate books to M number of students. There can be many ways or permutations to do so. In each permutation, one of the M students will be allocated the maximum number of pages. Out of all these permutations, the task is to find that particular permutation in which the maximum number of pages allocated to a student is minimum of those in all the other permutations and print this minimum value.
Each book will be allocated to exactly one student. Each student has to be allocated at least one book.
Note: Return -1 if a valid assignment is not possible, and allotment should be in contiguous order (see the explanation for better understanding).
Example 1:
Input:
N = 4
A[] = {12,34,67,90}
M = 2
Output:
113
Explanation:
Allocation can be done in following ways:
{12} and {34, 67, 90} Maximum Pages = 191
{12, 34} and {67, 90} Maximum Pages = 157
{12, 34, 67} and {90} Maximum Pages =113
Therefore, the minimum of these cases is
113, which is selected as the output.
Example 2:
Input:
N = 3
A[] = {15,17,20}
M = 2
Output:
32
Explanation:
Allocation is done as
{15,17} and {20}
Your Task:
You don't need to read input or print anything. Your task is to complete the function findPages() which takes 2 Integers N, and m and an array A[] of length N as input and returns the expected answer.
Expected Time Complexity: O(NlogN)
Expected Auxilliary Space: O(1)
Constraints:
1 <= N <= 105
1 <= A [ i ] <= 106
1 <= M <= 105
*/
/*
-----------TESTCASE------------
1
4
12 34 67 90
2
----------+--------+-----------
OUTPUT: 113
--------------------------------
Input:
8
250 74 159 181 23 45 129 174
6
Its Correct output is:
250
*/
import java.io.*;
import java.util.*;
class minimumNumberOfPages {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int m=sc.nextInt();
System.out.println(findPages(a,n,m));
}
}
public static int findPages(int[]a,int n,int m)
{
if(m>n) return -1 ; //If number of students is greater
int b = 0,e = 0;
for(int i:a)
e += i;
int min = e;
while(b<=e){
int mid = b + (e-b)/2;
//System.out.println(mid);
if(isValid(a,n,m,mid))
{
e = mid-1;
min = Math.min(min,mid);
} else {
b = mid + 1;
}
}
return min;
}
static boolean isValid(int[]a,int n,int m,int k){
int curr = 0;
m--;
for(int i:a){
if(i>k) return false;
//System.out.println("curr:"+curr);
curr += i;
if(curr>k){
m--; //Reduce number of students
curr = i; //Reinitialize curr for nxt student and consider this value a[i] for next one
}
if(m<0)return false;
}
//System.out.println("m = :"+m);
return true;
}//---isValid(args...) ends here
}//class ends