Print indices of array elements whose removal makes the sum of odd and even | Function to find indices of array elements whose removal makes the sum of odd and even indexed array elements equal ; Stores size of array ; Store prefix sum of odd index array elements ; Store prefix sum of even index array elements ; Update even [ 0 ] ; Traverse the given array ; Update odd [ i ] ; Update even [ i ] ; If the current index is an even number ; Update even [ i ] ; If the current index is an odd number ; Update odd [ i ] ; Check if at least one index found or not that satisfies the condition ; Store odd indices sum by removing 0 - th index ; Store even indices sum by removing 0 - th index ; If p and q are equal ; Traverse the array arr ; If i is an even number ; Update p by removing the i - th element ; Update q by removing the i - th element ; Update q by removing the i - th element ; Update p by removing the i - th element ; If odd index values sum is equal to even index values sum ; Set the find variable ; Print the current index ; If no index found ; Print not possible ; Driver Code
def removeIndicesToMakeSumEqual ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE odd = [ 0 ] * N ; NEW_LINE even = [ 0 ] * N ; NEW_LINE even [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT odd [ i ] = odd [ i - 1 ] ; NEW_LINE even [ i ] = even [ i - 1 ] ; NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT even [ i ] += arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT odd [ i ] += arr [ i ] ; NEW_LINE DEDENT DEDENT find = False ; NEW_LINE p = odd [ N - 1 ] ; NEW_LINE q = even [ N - 1 ] - arr [ 0 ] ; NEW_LINE if ( p == q ) : NEW_LINE INDENT print ( "0 ▁ " ) ; NEW_LINE find = True ; NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT p = even [ N - 1 ] - even [ i - 1 ] - arr [ i ] + odd [ i - 1 ] ; NEW_LINE q = odd [ N - 1 ] - odd [ i - 1 ] + even [ i - 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT q = odd [ N - 1 ] - odd [ i - 1 ] - arr [ i ] + even [ i - 1 ] ; NEW_LINE p = even [ N - 1 ] - even [ i - 1 ] + odd [ i - 1 ] ; NEW_LINE DEDENT if ( p == q ) : NEW_LINE INDENT find = True ; NEW_LINE print ( i , end = " " ) ; NEW_LINE DEDENT DEDENT if ( find == False ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 6 , 2 ] ; NEW_LINE removeIndicesToMakeSumEqual ( arr ) ; NEW_LINE DEDENT