Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Prthe maximum score ; Driver Code
def currSubArrayScore ( a , b , l , r ) : NEW_LINE INDENT straightScore = 0 NEW_LINE reverseScore = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT straightScore += a [ i ] * b [ i ] NEW_LINE reverseScore += a [ r - ( i - l ) ] * b [ i ] NEW_LINE DEDENT return max ( straightScore , reverseScore ) NEW_LINE DEDENT def maxScoreSubArray ( a , b , n ) : NEW_LINE INDENT res = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT currScore = currSubArrayScore ( a , b , i , j ) NEW_LINE if ( currScore > res ) : NEW_LINE INDENT res = currScore NEW_LINE start = i NEW_LINE end = j NEW_LINE DEDENT DEDENT DEDENT print ( res ) NEW_LINE DEDENT A = [ 13 , 4 , 5 ] NEW_LINE B = [ 10 , 22 , 2 ] NEW_LINE N = len ( A ) NEW_LINE maxScoreSubArray ( A , B , N ) NEW_LINE