Minimize count of flips required to make sum of the given array equal to 0 | Initialize dp [ ] [ ] ; Function to find the minimum number of operations to make sum of A [ ] 0 ; Initialize answer ; Base case ; Otherwise , return 0 ; Pre - computed subproblem ; Recurrence relation for finding the minimum of the sum of subsets ; Return the result ; Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0 ; Find the sum of array ; Initialise [ , ] dp with - 1 ; No solution exists ; Otherwise ; If sum is odd , no subset is possible ; Driver Code ; Function Call
let dp = [ ] ; for ( var i = 0 ; i < 2001 ; i ++ ) { dp [ i ] = [ ] ; for ( var j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } function solve ( A , i , sum , N ) { let res = 2001 ; if ( sum < 0 || ( i == N && sum != 0 ) ) { return 2001 ; } if ( sum == 0 i >= N ) { dp [ i ] [ sum ] = 0 ; return dp [ i ] [ sum ] ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } res = Math . min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) ; dp [ i ] [ sum ] = res ; return dp [ i ] [ sum ] ; } function minOp ( A , N ) { let sum = 0 ; for ( let i = 0 ; i < A . length ; i ++ ) { sum += A [ i ] ; } if ( sum % 2 == 0 ) { let dp = [ ] ; for ( var i = 0 ; i < 2001 ; i ++ ) { dp [ i ] = [ ] ; for ( var j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let ans = solve ( A , 0 , Math . floor ( sum / 2 ) , N ) ; if ( ans < 0 ans > N ) { document . write ( " " ) ; } else { document . write ( ans , " " ) ; } } else { document . write ( " " ) ; } } let A = [ 2 , 3 , 1 , 4 ] ; let N = A . length ; minOp ( A , N ) ;