If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum .
if ( i % 2 != 0 ) : NEW_LINE INDENT Sum -= n NEW_LINE if ( Sum % 2 != 0 ) : NEW_LINE INDENT ans . append ( sign * i ) NEW_LINE Sum += i NEW_LINE i += 1 NEW_LINE DEDENT ans [ int ( Sum / 2 ) - 1 ] *= - 1 NEW_LINE DEDENT else : NEW_LINE