Check if array sum can be made K by three operations on it | Java Program to find if Array can have a sum of K by applying three types of possible operations on it ; Check if it is possible to achieve a sum with three operation allowed . ; If sum is negative . ; If going out of bound . ; If sum is achieved . ; If the current state is not evaluated yet . ; Replacing element with negative value of the element . ; Substracting index number from the element . ; Adding index number to the element . ; Wrapper Function ; Driver Code
class GFG { static int MAX = 100 ; static int check ( int i , int sum , int n , int k , int a [ ] , int dp [ ] [ ] ) { if ( sum <= 0 ) { return 0 ; } if ( i >= n ) { if ( sum == k ) { return 1 ; } return 0 ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } dp [ i ] [ sum ] = check ( i + 1 , sum - 2 * a [ i ] , n , k , a , dp ) | check ( i + 1 , sum , n , k , a , dp ) ; dp [ i ] [ sum ] = check ( i + 1 , sum - ( i + 1 ) , n , k , a , dp ) | dp [ i ] [ sum ] ; dp [ i ] [ sum ] = check ( i + 1 , sum + i + 1 , n , k , a , dp ) | dp [ i ] [ sum ] ; return dp [ i ] [ sum ] ; } static int wrapper ( int n , int k , int a [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return check ( 0 , sum , n , k , a , dp ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 5 ; if ( wrapper ( n , k , a ) == 1 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }