Modify array by making all array elements equal to 0 by subtracting K^i from an array element in every i-th step
Given an array arr[] of size N, the task is to check if it is possible to convert all array elements to 0s, by subtracting Ki from an array element, in the ith step. If it is possible to do so, then print “Yes“. Otherwise, print “No“.
Examples:
Input: N = 5, K = 2, arr[] = {8, 0, 3, 4, 80}
Output: Yes
Explanation:
One possible sequence of operations is as follows:
- Subtract 20 from arr[2]( = 3 ). Thereafter, the array modifies to, arr[] = {8, 0, 2, 4, 32}.
- Subtract 21 from arr[2]( = 2 ). Thereafter, the array modifies to, arr[] = {8, 0, 0, 4, 32}.
- Subtract 22 from arr[3]( = 4 ). Thereafter, the array modifies to, arr[] = {8, 0, 0, 0, 32}.
- Subtract 23 from arr[1]( = 8 ). Thereafter, the array modifies to, arr[] = {0, 0, 0, 0, 32}.
- Do not subtract 24 from any array element.
- Subtract 25 from arr[4]( = 32 ). Thereafter, the array modifies to, arr[] = {0, 0, 0, 0, 0}.
Input: N = 3, K = 2, arr[] = {0, 1, 3}
Output: No
Approach: Follow the steps below to solve the problem:
- Initialize a vector, say V, to store all powers of K possible.
- Also, initialize a map<int, int>, say MP, to store if a power of K has been used or not.
- Initialize a variable, say X as 1, to store the count of powers of K.
- Iterate until X is less than INT_MAX and perform the following steps:
- Push the value of X into the vector.
- Multiply X by K.
- Iterate over the range [0, N – 1] using a variable i and perform the following steps:
- Iterate over the vector, V in reverse, using a variable j, and perform the following steps:
- If arr[i] is greater than V[j] and MP[V[j]] is 0, then subtract V[j] from arr[i].
- Update MP[V[j]] to 1.
- If arr[i] is not equal to 0, then break from the loop.
- If i is less than N, then print “No”, as the array elements can not be made 0. Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def isMakeZero(arr, N, K):
MP = {}
V = []
X = 1
while (X > 0 and X < 10 * * 20 ):
V.append(X)
X * = K
for i in range ( 0 , N, 1 ):
for j in range ( len (V) - 1 , - 1 , - 1 ):
if (V[j] not in MP and V[j] < = arr[i]):
arr[i] - = V[j]
MP[V[j]] = 1
if (arr[i] ! = 0 ):
break
if (i < N - 1 ):
return "No"
else :
return "Yes"
arr = [ 8 , 0 , 3 , 4 , 80 ]
N = len (arr)
K = 2
print (isMakeZero(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string isMakeZero( int [] arr, int N, int K)
{
Dictionary< int , int > MP = new Dictionary< int , int >();
List< int > V = new List< int >();
int X = 1;
int i;
while (X > 0 && X < Int32.MaxValue) {
V.Add(X);
X *= K;
}
for (i = 0; i < N; i++) {
for ( int j = V.Count - 1; j >= 0; j--) {
if (MP.ContainsKey(V[j]) == false && 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" ;
}
public static void Main(String[] args)
{
int [] arr = { 8, 0, 3, 4, 80 };
int N = arr.Length;
int K = 2;
Console.WriteLine(isMakeZero(arr, N, K));
}
}
|
Javascript
<script>
function isMakeZero(arr, N, K) {
let MP = new Map();
let V = [];
let X = 1;
let i;
while (X > 0 && X < Number.MAX_VALUE) {
V.push(X);
X *= K;
}
for (i = 0; i < N; i++) {
for (let j = V.length - 1; j >= 0; j--) {
if (MP.has(V[j]) == false && V[j] <= arr[i]) {
arr[i] -= V[j];
MP.set(V[j], 1);
}
}
if (arr[i] != 0)
break ;
}
if (i < N)
return "No" ;
else
return "Yes" ;
}
let arr = [8, 0, 3, 4, 80];
let N = arr.length;
let K = 2;
document.write(isMakeZero(arr, N, K));
</script>
|
Time Complexity: O(N* logK(INT_MAX))
Auxiliary Space: O(logK(INT_MAX))
Last Updated :
16 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...