Queries to find minimum sum of array elements from either end of an array | Function to calculate the minimum sum from either end of the arrays for the given queries ; Traverse the query [ ] array ; Stores sum from start and end of the array ; Calculate distance from start ; Calculate distance from end ; Driver Code
def calculateQuery ( arr , N , query , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT X = query [ i ] NEW_LINE sum_start = 0 NEW_LINE sum_end = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT sum_start += arr [ j ] NEW_LINE if ( arr [ j ] == X ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum_end += arr [ j ] NEW_LINE if ( arr [ j ] == X ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( min ( sum_end , sum_start ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 30 ] NEW_LINE queries = [ 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( queries ) NEW_LINE calculateQuery ( arr , N , queries , M ) NEW_LINE DEDENT