Queries to find the minimum array sum possible by removing elements from either end | Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Prthe minimum of the two as the answer ; Driver Code ; Function Call
def minSum ( arr , N , Q , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT val = Q [ i ] NEW_LINE front , rear = 0 , 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT front += arr [ j ] NEW_LINE if ( arr [ j ] == val ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT rear += arr [ j ] NEW_LINE if ( arr [ j ] == val ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( min ( front , rear ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ 7 , 6 ] NEW_LINE M = len ( Q ) NEW_LINE minSum ( arr , N , Q , M ) NEW_LINE DEDENT