- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Allocate minimum number of pages in C++
Allocate a minimum number of pages is a programming problem. Let's discuss this problem in detail and see what can be the solution to it.
Statement
You are given the number of pages of n different books. Also, there are m students to whom the books are to be assigned. The books are arranged in ascending order of the number of pages. And every student can be assigned some consecutive books. The program should return the maximum number of pages read by a student which should be minimum.
Let's take an example to understand this problem in a better way,
Input : books[] = {13 , 43, 65, 87, 92} m = 2 Output : 179
Explanation
In this problem, we have two students who are reading books. So, there can be the following ways to distribute books between them.
CASE 1 − [13] , [43, 65, 87, 92 ]
This makes the maximum number of pages read by a student is 13 / 287
CASE 2 − [13, 43] , [65, 87,92]
This makes the maximum number of pages read by a student is 56/ 244
CASE 3 − [13, 43 , 65] , [87, 92]
This makes the maximum number of pages read by a student is 121 / 179
CASE 4 − [13, 43 , 65 , 87] , [92]
This makes the maximum number of pages read by a student is 208 / 92
Out of all these 4 cases, the result is 179
This example must have made the problem clear to you. Now, let's understand the logic behind it and create a program for it.
To solve this problem an easy approach is using the binary search algorithm. For this binary search approach, initialize minimum and a maximum number of pages as 0 and sum of pages of all books. And then fix the mid of these values as the intermediate result which will change as the algo proceeds further.
Now, using the mid-value we will try to find the possibility to find the final solution. If the current mid has chances to become a solution, then the lower half i.e minimum to mid is searching. If this case is not true then the other half i.e. mid to maximum is searched.
This method can be used to find the solution to this problem but as the number of students increases this algorithm tends to provide a less reliable solution.
Example
#include<bits/stdc++.h> using namespace std; bool isPossible(int arr[], int n, int m, int curr_min) ; int min_pages(int arr[], int n, int m) ; int main(){ int n = 5; int books[] = {13 , 43, 65, 87, 92}; cout<<"The number of page in books are :\n"; for(int i = 0 ; i< n; i++){ cout<<books[i]<<"\t"; } int m = 2; cout<<"\nMinimum number of pages = "<<min_pages(books, n, m)<<endl; return 0; } bool isPossible(int arr[], int n, int m, int curr_min){ int studentsRequired = 1; int curr_sum = 0; for (int i = 0; i < n; i++){ if (arr[i] > curr_min) return false; if (curr_sum + arr[i] > curr_min){ studentsRequired++; curr_sum = arr[i]; if (studentsRequired > m) return false; } else curr_sum += arr[i]; } return true; } int min_pages(int arr[], int n, int m){ long long sum = 0; if (n < m) return -1; for (int i = 0; i < n; i++) sum += arr[i]; int minimum = 0, maximum = sum; int result = INT_MAX; while (minimum <= maximum){ int mid = (minimum + maximum) / 2; if (isPossible(arr, n, m, mid)){ result = min(result, mid); maximum = mid - 1; } else minimum = mid + 1; } return result; }
Output
The number of page in books are : 13 43 65 87 92 Minimum number of pages = 179
- Related Articles
- Minimum Number of Refueling Stops in C++
- Minimum Number of Frogs Croaking in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Minimum number of Square Free Divisors in C++
- Minimum number using set bits of a given number in C++
- How to dynamically allocate a 2D array in C?
- Minimum number of stops from given path in C++
- Minimum Number of Arrows to Burst Balloons in C++
- Minimum Number of K Consecutive Bit Flips in C++
- DoubleBuffer allocate() method in Java
- ShortBuffer allocate() method in Java
- ByteBuffer allocate() method in Java
- CharBuffer allocate() method in Java
- IntBuffer allocate() method in Java
- FloatBuffer allocate() method in Java
