Allocate minimum number of pages | Utility function to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return False ; else update curr_sum ; function to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; update result to current distribution as it 's the best we have found till now. ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Number of pages in books ; m = 2 No . of students
def isPossible ( arr , n , m , curr_min ) : NEW_LINE INDENT studentsRequired = 1 NEW_LINE curr_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > curr_min ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( curr_sum + arr [ i ] > curr_min ) : NEW_LINE INDENT studentsRequired += 1 NEW_LINE curr_sum = arr [ i ] NEW_LINE if ( studentsRequired > m ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findPages ( arr , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( n < m ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT start , end = 0 , sum NEW_LINE result = 10 ** 9 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( isPossible ( arr , n , m , mid ) ) : NEW_LINE INDENT result = mid NEW_LINE end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 12 , 34 , 67 , 90 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ number ▁ of ▁ pages ▁ = ▁ " , findPages ( arr , n , m ) ) NEW_LINE