Find maximum topics to prepare in order to pass the exam | Python3 implementation of the approach ; Function to return the maximum marks by considering topics which can be completed in the given time duration ; If we are given 0 time then nothing can be done So all values are 0 ; If we are given 0 topics then the time required will be 0 for sure ; Calculating the maximum marks that can be achieved under the given time constraints ; If time taken to read that topic is more than the time left now at position j then do no read that topic ; Two cases arise : 1 ) Considering current topic 2 ) Ignoring current topic We are finding maximum of ( current topic weightage + topics which can be done in leftover time - current topic time ) and ignoring current topic weightage sum ; Moving upwards in table from bottom right to calculate the total time taken to read the topics which can be done in given time and have highest weightage sum ; It means we have not considered reading this topic for max weightage sum ; Adding the topic time ; Evaluating the left over time after considering this current topic ; One topic completed ; It contains the maximum weightage sum formed by considering the topics ; Condition when exam cannot be passed ; Return the marks that can be obtained after passing the exam ; Driver code ; Number of topics , hours left and the passing marks ; n + 1 is taken for simplicity in loops Array will be indexed starting from 1
import numpy as np NEW_LINE def MaximumMarks ( marksarr , timearr , h , n , p ) : NEW_LINE INDENT no_of_topics = n + 1 ; NEW_LINE total_time = h + 1 ; NEW_LINE T = np . zeros ( ( no_of_topics , total_time ) ) ; NEW_LINE for i in range ( no_of_topics ) : NEW_LINE INDENT T [ i ] [ 0 ] = 0 ; NEW_LINE DEDENT for j in range ( total_time ) : NEW_LINE INDENT T [ 0 ] [ j ] = 0 ; NEW_LINE DEDENT for i in range ( 1 , no_of_topics ) : NEW_LINE INDENT for j in range ( 1 , total_time ) : NEW_LINE INDENT if ( j < timearr [ i ] ) : NEW_LINE INDENT T [ i ] [ j ] = T [ i - 1 ] [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT T [ i ] [ j ] = max ( marksarr [ i ] + T [ i - 1 ] [ j - timearr [ i ] ] , T [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT i = no_of_topics - 1 ; j = total_time - 1 ; NEW_LINE sum = 0 ; NEW_LINE while ( i > 0 and j > 0 ) : NEW_LINE INDENT if ( T [ i ] [ j ] == T [ i - 1 ] [ j ] ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT sum += timearr [ i ] ; NEW_LINE j -= timearr [ i ] ; NEW_LINE i -= 1 ; NEW_LINE DEDENT DEDENT marks = T [ no_of_topics - 1 ] [ total_time - 1 ] ; NEW_LINE if ( marks < p ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; h = 10 ; p = 10 ; NEW_LINE marksarr = [ 0 , 6 , 4 , 2 , 8 ] ; NEW_LINE timearr = [ 0 , 4 , 6 , 2 , 7 ] ; NEW_LINE print ( MaximumMarks ( marksarr , timearr , h , n , p ) ) ; NEW_LINE DEDENT