human
Count of Ways to obtain given Sum from the given Array elements | Function to count the number of ways.
Base Case : Reached the end of the array.
Sum is equal to the required sum.
Recursively check if required sum can be obtained by adding current element or by subtracting the current index element.
Function to call dfs ( ) to calculate the number of ways.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count of Ways to obtain given Sum from the given Array elements and the other steps required.
 def dfs ( nums , S , curr_sum , index ) :
	if ( index == len ( nums ) ) :
		if ( S == curr_sum ) :
			return 1 ;
		else :
			return 0 ;
	return ( dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ) ;
def findWays ( nums , S ) :
	return dfs ( nums , S , 0 , 0 ) ;
if __name__ == ' _ _ main _ _ ' :
	S = 3 ;
	arr = [ 1 , 2 , 3 , 4 , 5 ] ;
	answer = findWays ( arr , S ) ;
	print ( answer ) ;