Problem of the day
Given an array āarrā of integer numbers, āarr[i]ā represents the number of pages in the āi-thā book.
There are āmā number of students, and the task is to allocate all the books to the students.
Allocate books in such a way that:
1. Each student gets at least one book.
2. Each book should be allocated to only one student.
3. Book allocation should be in a contiguous manner.
You have to allocate the book to āmā students such that the maximum number of pages assigned to a student is minimum.
If the allocation of books is not possible, return -1.
Input: ānā = 4 āmā = 2
āarrā = [12, 34, 67, 90]
Output: 113
Explanation: All possible ways to allocate the ā4ā books to '2' students are:
12 | 34, 67, 90 - the sum of all the pages of books allocated to student 1 is ā12ā, and student two is ā34+ 67+ 90 = 191ā, so the maximum is āmax(12, 191)= 191ā.
12, 34 | 67, 90 - the sum of all the pages of books allocated to student 1 is ā12+ 34 = 46ā, and student two is ā67+ 90 = 157ā, so the maximum is āmax(46, 157)= 157ā.
12, 34, 67 | 90 - the sum of all the pages of books allocated to student 1 is ā12+ 34 +67 = 113ā, and student two is ā90ā, so the maximum is āmax(113, 90)= 113ā.
We are getting the minimum in the last case.
Hence answer is ā113ā.
The first line contains two space-separated integers ānā denoting the number of books and āmā denotes the number of students.
The second line contains ānā space-separated integers denoting the number of pages in each of ānā books.
Output Format:
Return the integer as described above.
Note:
Do not print anything, just return the maximum number of pages that are assigned to a student is minimum.
4 2
12 34 67 90
113
All possible ways to allocate the ā4ā books to '2' students are:
12 | 34, 67, 90 - the sum of all the pages of books allocated to student 1 is ā12ā, and student two is ā34+ 67+ 90 = 191ā, so the maximum is āmax(12, 191)= 191ā.
12, 34 | 67, 90 - the sum of all the pages of books allocated to student 1 is ā12+ 34 = 46ā, and student two is ā67+ 90 = 157ā, so the maximum is āmax(46, 157)= 157ā.
12, 34, 67 | 90 - the sum of all the pages of books allocated to student 1 is ā12+ 34 +67 = 113ā, and student two is ā90ā, so the maximum is āmax(113, 90)= 113ā.
We are getting the minimum in the last case.
Hence answer is ā113ā.
5 4
25 46 28 49 24
71
All possible ways to allocate the ā5ā books to '4' students are:
25 | 46 | 28 | 49 24 - the sum of all the pages of books allocated to students 1, 2, 3, and 4 are '25', '46', '28', and '73'. So the maximum is '73'.
25 | 46 | 28 49 | 24 - the sum of all the pages of books allocated to students 1, 2, 3, and 4 are '25', '46', '77', and '24'. So the maximum is '77'.
25 | 46 28 | 49 | 24 - the sum of all the pages of books allocated to students 1, 2, 3, and 4 are '25', '74', '49', and '24'. So the maximum is '74'.
25 46 | 28 | 49 | 24 - the sum of all the pages of books allocated to students 1, 2, 3, and 4 are '71', '28', '49', and '24'. So the maximum is '71'.
We are getting the minimum in the last case.
Hence answer is ā71ā.
The expected time complexity is O(n * log(s)), where ānā is the number of integers in the array āarrā and āsā is the sum of all the elements of āarrā.
2 <= 'n' <= 10 ^ 3
1 <= 'm' <= 10 ^ 3
1 <= 'arr[i]' <= 10 ^ 9
The sum of all arr[i] does not exceed 10 ^ 9.
Where ānā denotes the number of books and āmā denotes the number of students. āarr[i]ā denotes an element at position āiā in the sequence.
Time limit: 1 second
Try all possible minimum number of pages.
The basic idea is that, try each and every possible value that can be answered. It can be from ā1ā to the sum of āarrā.
Iterate a loop āiā, if sum of some contiguous books pages is less than or equals to mid then assign this sou to a single student and check for remain student and at the end if is possible then can say it is possible that āmidā number of pages is possible to assign āmā student.
O(n * s), where ānā is the number of integers in the array āarrā and āsā is the sum of all the elements of āarrā.
We are using two nested loops of size āsā and ānā.
Therefore the time complexity is O(n * s).
O(1)
We are using constant space.
Therefore the space complexity is O(1).