Modify array to another given array by replacing array elements with the sum of the array | Java program to implement the above approach ; Function to check if the arr [ ] can be converted to target [ ] by replacing any element in arr [ ] by the sum of arr [ ] ; Store the maximum element ; Store the index of the maximum element ; Traverse the array target [ ] ; If current element is greater than max ; If max element is 1 ; Traverse the array , target [ ] ; If current index is not equal to maximum element index ; Update max ; If max is less than or equal to 0 , ; Update the maximum element ; Recursively call the function ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static boolean isPossible ( int [ ] target ) { int max = 0 ; int index = 0 ; for ( int i = 0 ; i < target . length ; i ++ ) { if ( max < target [ i ] ) { max = target [ i ] ; index = i ; } } if ( max == 1 ) return true ; for ( int i = 0 ; i < target . length ; i ++ ) { if ( i != index ) { max -= target [ i ] ; if ( max <= 0 ) return false ; } } target [ index ] = max ; return isPossible ( target ) ; } public static void main ( String [ ] args ) { int [ ] target = { 9 , 3 , 5 } ; boolean res = isPossible ( target ) ; if ( res ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }