Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver Code
def checkReverse ( leftSum , rightSum ) : NEW_LINE INDENT rev = 0 NEW_LINE temp = rightSum NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT rev = ( rev * 10 ) + ( temp % 10 ) NEW_LINE temp //= 10 NEW_LINE DEDENT if ( rev == leftSum ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findIndex ( arr , N ) : NEW_LINE INDENT rightSum = 0 NEW_LINE leftSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT rightSum += arr [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT rightSum -= arr [ i ] NEW_LINE if ( checkReverse ( leftSum , rightSum ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT leftSum += arr [ i ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 7 , 3 , 6 , 4 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N ) ) NEW_LINE DEDENT