Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid poremains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code
def maxScoreSubArray ( a , b , n ) : NEW_LINE INDENT res = 0 NEW_LINE for mid in range ( n ) : NEW_LINE INDENT straightScore = a [ mid ] * b [ mid ] NEW_LINE reverseScore = a [ mid ] * a [ mid ] NEW_LINE prev = mid - 1 NEW_LINE next = mid + 1 NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE while ( prev >= 0 and next < n ) : NEW_LINE INDENT straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) NEW_LINE reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE prev -= 1 NEW_LINE next += 1 NEW_LINE DEDENT straightScore = 0 NEW_LINE reverseScore = 0 NEW_LINE prev = mid - 1 NEW_LINE next = mid NEW_LINE while ( prev >= 0 and next < n ) : NEW_LINE INDENT straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) NEW_LINE reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE prev -= 1 NEW_LINE next += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 13 , 4 , 5 ] NEW_LINE B = [ 10 , 22 , 2 ] NEW_LINE N = len ( A ) NEW_LINE maxScoreSubArray ( A , B , N ) NEW_LINE DEDENT