Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isMakeZero ( int arr [ ] , int N , int K ) { map < int , int > MP ; vector < int > V ; int X = 1 ; int i ; while ( X > 0 && X < INT_MAX ) { V . push_back ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . size ( ) - 1 ; j >= 0 ; j -- ) { if ( MP [ V [ j ] ] == 0 && V [ j ] <= arr [ i ] ) { arr [ i ] -= V [ j ] ; MP [ V [ j ] ] = 1 ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } int main ( ) { int arr [ ] = { 8 , 0 , 3 , 4 , 80 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << isMakeZero ( arr , N , K ) ; return 0 ; }