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 ) { sum -= n ; if ( sum % 2 != 0 ) { ans . Add ( sign * i ) ; sum += i ; i ++ ; } int a = ans [ ( ( sum / 2 ) - 1 ) ] ; ans . RemoveAt ( ( sum / 2 ) - 1 ) ; ans . Insert ( ( ( sum / 2 ) - 1 ) , a * ( - 1 ) ) ; } else {