Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | Java program for the above approach ; Function to check whether all array elements can be made zero or not ; Stores if a power of K has already been subtracted or not ; Stores all the Kth power ; Iterate until X is less than INT_MAX ; Traverse the array arr [ ] ; Iterate over the range [ 0 , M ] ; If MP [ V [ j ] ] is 0 and V [ j ] is less than or equal to arr [ i ] ; If arr [ i ] is not 0 ; If i is less than N ; Otherwise , ; Driver code
import java . util . ArrayList ; import java . util . HashMap ; class GFG { static String isMakeZero ( int arr [ ] , int N , int K ) { HashMap < Integer , Integer > MP = new HashMap < Integer , Integer > ( ) ; ArrayList < Integer > V = new ArrayList < Integer > ( ) ; int X = 1 ; int i ; while ( X > 0 && X < Integer . MAX_VALUE ) { V . add ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . size ( ) - 1 ; j >= 0 ; j -- ) { if ( MP . containsKey ( V . get ( j ) ) == false && V . get ( j ) <= arr [ i ] ) { arr [ i ] -= V . get ( j ) ; MP . put ( V . get ( j ) , 1 ) ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 0 , 3 , 4 , 80 } ; int N = arr . length ; int K = 2 ; System . out . println ( isMakeZero ( arr , N , K ) ) ; } }