Allocate minimum number of pages | Practice | GeeksforGeeks

You are given N number of books. Every ith book has Ai number of pages. The books are arranged in ascending order.

You have to allocate contiguous 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 the 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
 
-1
abhinayabhi1413 days ago
class Solution {   //Function to find minimum number of pages.   public static int findPages(int[]A,int N,int M)   {       //Your code here         int low=0,high=0;      for(int i=0;i<A.length;i++){          low = Math.max(low,A[i]);          high += A[i];      }      while(low<high){          int mid = low +(high-low)/2;          if(sub(A,mid)>M){              low = mid+1;          }          else{              high = mid;          }                }      return low;        }  static int sub(int[] nums,int tar){      int piece = 1,sum=0;      for(int i=0;i<nums.length;i++){          if(sum+nums[i]<=tar){              sum += nums[i];          }          else{              piece++;              sum = nums[i];          }      }      return piece;   }};
-1
chessnoobdj3 days ago
C++ binary search
bool check(int A[], int N, int M, int max_sum){
    int cnt = 1, s = 0;
    for(int i=0; i<N; i++){
        if(A[i] > max_sum)
            return false;
        if(s+A[i] > max_sum){
            s = 0;
            cnt += 1;
        }
        s += A[i];
    }
    return cnt <= M;
}
int findPages(int A[], int N, int M) 
{
    if(M > N)
        return -1;
    long l = 1, r = accumulate(A, A+N, 0);
    while(l < r){
        int m = (l+r)/2;
        if(check(A, N, M, m))
            r = m;
        else
            l = m+1;
    }
    return l;
}
0
bhanukode55 days ago
int findPages(int *a, int books, int students) {
    int high = 0;
    int low = 0;
    for (int i = 0; i < books; ++i) {
        high += a[i];
        low = max(low, a[i]);
    }

    int ans = 0;
    while(low <= high) {
        int mid = (low + high) / 2;
        
        if (isFeasible(a, books, students, mid)) {
            high = mid - 1;
            ans = mid;
        }
        
        else
            low = mid + 1;
    }

    return ans;
}

bool isFeasible(int *a, int books, int students, int limit) {
    int pages = 0;
    int parts = 1;

    for (int i = 0; i < books; ++i) {
        if (pages + a[i] > limit) {
            parts++;
            pages = 0;
        }
        pages += a[i];
    }

    return (parts <= students);
}

0
gitansh18saharan6 days ago
JAVA :
 
class Solution {   //Function to find minimum number of pages.      public static int maxOf(int[]A){       int n = A.length;       return A[n-1];   }   public static int sumOf(int[] A){       int high=0;       for(int i =0; i<A.length ;i++){           high += A[i];       }       return high;   }   public static int findPages(int[]A,int N,int M)   {       //Your code here       int min = maxOf(A);       int max = sumOf(A);              int res =0;       while(min <= max){           int mid = (max+min)/2;                      if(isfeasible(A,M,mid)){               res = mid;               max = mid-1;           }           else{               min = mid+1;           }       }       return res;          }      static boolean isfeasible(int[]A, int M, int res){       int student =1,sum =0;       for(int i =0; i<A.length;i++){           if(sum + A[i]>res){               student ++;               sum = A[i];           }           else{               sum +=A[i];           }       }       return student <= M;   }
0
nwtwfzjxcz30rxmy5x7khnj7h2dkb3ujyo0z4zqv6 days ago
JAVA SOLUTION
 
class Solution {   //Function to find minimum number of pages.   public static int findPages(int[]A,int N,int M)   {       int low=0,high=0;       for(int i=0;i<A.length;i++){           low = Math.max(low,A[i]);           high += A[i];       }       while(low<high){           int mid = low +(high-low)/2;           if(sub(A,mid)>M){               low = mid+1;           }           else{               high = mid;           }                  }       return low;          }   static int sub(int[] nums,int tar){       int piece = 1,sum=0;       for(int i=0;i<nums.length;i++){           if(sum+nums[i]<=tar){               sum += nums[i];           }           else{               piece++;               sum = nums[i];           }       }       return piece;   }};
0
hum_toh_ram_bharose1 week ago
       if(N<M)      return -1;      int res=-1;      int max=0; int sum=0;      for(int i=0;i<N;i++){      sum=sum+A[i];      if(max<A[i])      max=A[i];}      int start=max; int end=sum;      while(start<=end){          int mid= start+(end-start)/2;      if(isValid(A,N,M,mid)){      res=mid;         end=mid-1; }         else         start=mid+1;      }      return res;   }   bool isValid(int A[],int N,int M,int mid){       int student=1; int sum=0;       for(int i=0;i<N;i++){           sum +=A[i];           if(sum>mid){               student++;               sum=A[i];           }           if(student>M)           return false;       }       return true;   }
+1
rohitjug19csPremium1 week ago
JAVA Solution : 
public static int findPages(int[]a, int n, int m)
    {
       int low = a[n-1];
       int high = 0;
       for(int ele : a)
          high += ele;
          
       int ans = -1;
       
       while(low <= high){
         int mid = (low + high)/2;
         
         int pc=0;
         int sc=1;
         for(int i=0; i<n; i++){
            if(pc + a[i] <= mid)pc += a[i];
            else{
                pc = a[i];
                sc++;
            }
         }
         
         if(sc <= m){
             ans = mid;
             high = mid-1;
         }
         else low = mid + 1;
       }
       
       return ans;
    }
0
putyavka1 week ago
struct Prob { int v, c; };
Prob prob(int A[], int N, int v) {
    int c = 1;
    for (int i = 0, s = 0; i < N; i++)
        if (s + A[i] <= v) s += A[i];
        else { s = A[i]; c++; }
    return {v, c};
}
int findPages(int A[], int N, int M) {
    if (M > N) return -1;
    int e = A[N-1];
    auto mn = prob(A, N, e);
    auto mx = prob(A, N, e*min(N,0x7fffffff/e));
    while (mn.v+1 < mx.v) {
        auto m = prob(A, N, (mx.v+mn.v)>>1);
        if (m.c <= M) mx = m;
        else mn = m;
    }
    return mn.c<=M ? mn.v : mx.v;
}
+1
kake13371 week ago
    int findPages(int A[], int N, int M) 
   {
      if(M>N)
      return -1;
      int l=0,h=0,r;
      for(int i=0;i<N;i++)
      h+=A[i];
      while(l<=h)
      {
          int mid=l+(h-l)/2;
          int p=M,csum=0,i=0;
          while(i<N)
          {
              csum+=A[i];
              if(csum>mid)
              {
                  p--;
                  if(p==0)
                  break;
                  csum=0;
              }
              else
              i++;
          }
          if(p==0)
          l=mid+1;
          else
          {
              r=mid;
              h=mid-1;
          }
          
      }
      return r;
   }
+1
roboto7o32oo31 week ago
Clean C++ Solution ✨ ✨ (Two Approaches)
 
1. Dynamic Programming
Time Complexity = O(n^3)
Approach is kinda similar to Matrix Chain Multiplication
class Solution 
{
    public:
    
    int solve(int A[], int N, int M, vector<vector<int>>& memo){
        if(N==0 and M==0){
            return 0;
        }
        if(N==0 or M==0){
            return INT_MAX;
        }
        if(M>N){
            return INT_MAX;
        }
        if(memo[N][M] != -1){
            return memo[N][M];
        }
        
        memo[N][M] = INT_MAX;
        
        for(int i=N-1; i>=0; i--){
            memo[N][M] = min(memo[N][M], max(accumulate(A+i, A+N, 0), solve(A, i, M-1, memo)));
        }
        
        return memo[N][M];
    }
    
    
    int findPages(int A[], int N, int M) 
    {
        vector<vector<int>> memo(N+1, vector<int>(M+1, -1));
        return solve(A, N, M, memo);
    }
}
 
2. Lower Bound Binary Search
Time Complexity = O(nlogn)
watch strivers video for explanation
class Solution 
{
    public:
    
    bool isAllocatable(int A[], int N, int M, int P){
        /*
        try to allocate N books to students such that the pages per student is atmost P.
        returns true if we can allocate the books to number of students less than or equal to M.
        else returns false
        */
        int count = 1;
        int sum = 0;
        
        for(int i=0; i<N; i++){
            if(sum+A[i] <= P){
                sum += A[i];
            }
            else{
                if(A[i] > P){
                    return false;
                }
                count++;
                sum = A[i];
            }
        }
        
        return (count <= M) ? true : false;
    }
    
    
    int findPages(int A[], int N, int M)
    {
        int low = A[0];
        int high = accumulate(A, A+N, 0)+1;
        int mid;
        
        while(high > low){
            mid = (low+high)/2;
            
            if(isAllocatable(A, N, M, mid)){
                high = mid;
            }
            else{
                low = mid+1;
            }
        }
        
        return low;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.