Modify array by making all array elements equal to 0 by subtracting K^i from an array element in every i-th step
Last Updated :
16 Dec, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether all array
// elements can be made zero or not
string isMakeZero(int arr[], int N, int K)
{
// Stores if a power of K has
// already been subtracted or not
map<int, int> MP;
// Stores all the Kth power
vector<int> V;
int X = 1;
int i;
// Iterate until X is
// less than INT_MAX
while (X > 0 && X < INT_MAX) {
V.push_back(X);
X *= K;
}
// Traverse the array arr[]
for (i = 0; i < N; i++) {
// Iterate over the range [0, M]
for (int j = V.size() - 1; j >= 0; j--) {
// If MP[V[j]] is 0 and V[j]
// is less than or equal to arr[i]
if (MP[V[j]] == 0 && V[j] <= arr[i]) {
arr[i] -= V[j];
MP[V[j]] = 1;
}
}
// If arr[i] is not 0
if (arr[i] != 0)
break;
}
// If i is less than N
if (i < N)
return "No";
// Otherwise,
else
return "Yes";
}
// Driver code
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
// Java program for the above approach
import java.util.ArrayList;
import java.util.HashMap;
class GFG {
// Function to check whether all array
// elements can be made zero or not
static String isMakeZero(int arr[], int N, int K)
{
// Stores if a power of K has
// already been subtracted or not
HashMap<Integer, Integer> MP = new HashMap<Integer, Integer>();
// Stores all the Kth power
ArrayList<Integer> V = new ArrayList<Integer>();
int X = 1;
int i;
// Iterate until X is
// less than INT_MAX
while (X > 0 && X < Integer.MAX_VALUE) {
V.add(X);
X *= K;
}
// Traverse the array arr[]
for (i = 0; i < N; i++) {
// Iterate over the range [0, M]
for (int j = V.size() - 1; j >= 0; j--) {
// If MP[V[j]] is 0 and V[j]
// is less than or equal to arr[i]
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] is not 0
if (arr[i] != 0)
break;
}
// If i is less than N
if (i < N)
return "No";
// Otherwise,
else
return "Yes";
}
// Driver code
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));
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python Program for the above approach
# Function to check whether all array
# elements can be made zero or not
def isMakeZero(arr, N, K):
# Stores if a power of K has
# already been subtracted or not
MP = {}
# Stores all the Kth power
V = []
X = 1
# Iterate until X is
# less than INT_MAX
while (X > 0 and X < 10**20):
V.append(X)
X *= K
# Traverse the array arr[]
for i in range(0, N, 1):
# Iterate over the range [0, M]
for j in range(len(V) - 1, -1, -1):
# If MP[V[j]] is 0 and V[j]
# is less than or equal to arr[i]
if (V[j] not in MP and V[j] <= arr[i]):
arr[i] -= V[j]
MP[V[j]] = 1
# If arr[i] is not 0
if (arr[i] != 0):
break
# If i is less than N - 1
if (i < N - 1):
return "No"
# Otherwise,
else:
return "Yes"
# Driver code
arr = [8, 0, 3, 4, 80]
N = len(arr)
K = 2
print(isMakeZero(arr, N, K))
# This code is contributed by _saurabh_jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check whether all array
// elements can be made zero or not
static string isMakeZero(int[] arr, int N, int K)
{
// Stores if a power of K has
// already been subtracted or not
Dictionary<int,int> MP = new Dictionary<int,int>();
// Stores all the Kth power
List<int> V = new List<int>();
int X = 1;
int i;
// Iterate until X is
// less than INT_MAX
while (X > 0 && X < Int32.MaxValue) {
V.Add(X);
X *= K;
}
// Traverse the array arr[]
for (i = 0; i < N; i++) {
// Iterate over the range [0, M]
for (int j = V.Count - 1; j >= 0; j--) {
// If MP[V[j]] is 0 and V[j]
// is less than or equal to arr[i]
if (MP.ContainsKey(V[j]) == false && V[j] <= arr[i]) {
arr[i] -= V[j];
MP[V[j]] = 1;
}
}
// If arr[i] is not 0
if (arr[i] != 0)
break;
}
// If i is less than N
if (i < N)
return "No";
// Otherwise,
else
return "Yes";
}
// Driver Code
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));
}
}
// This code is contributed by splevel62.
JavaScript
<script>
// JavaScript Program for the above approach
// Function to check whether all array
// elements can be made zero or not
function isMakeZero(arr, N, K) {
// Stores if a power of K has
// already been subtracted or not
let MP = new Map();
// Stores all the Kth power
let V = [];
let X = 1;
let i;
// Iterate until X is
// less than INT_MAX
while (X > 0 && X < Number.MAX_VALUE) {
V.push(X);
X *= K;
}
// Traverse the array arr[]
for (i = 0; i < N; i++) {
// Iterate over the range [0, M]
for (let j = V.length - 1; j >= 0; j--) {
// If MP[V[j]] is 0 and V[j]
// is less than or equal to arr[i]
if (MP.has(V[j]) == false && V[j] <= arr[i]) {
arr[i] -= V[j];
MP.set(V[j], 1);
}
}
// If arr[i] is not 0
if (arr[i] != 0)
break;
}
// If i is less than N
if (i < N)
return "No";
// Otherwise,
else
return "Yes";
}
// Driver code
let arr = [8, 0, 3, 4, 80];
let N = arr.length;
let K = 2;
document.write(isMakeZero(arr, N, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N* logK(INT_MAX))
Auxiliary Space: O(logK(INT_MAX))
Similar Reads
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal Given an array arr[] consisting of N elements, the task is to find the minimum number of increments required to be performed on the given array such that after selecting any array element at any index and splitting its value to the other array elements makes all other N - 1 elements equal. Examples:
6 min read
Construct original array starting with K from an array of XOR of all elements except elements at same index Given an array A[] consisting of N integers and first element of the array B[] as K, the task is to construct the array B[] from A[] such that for any index i, A[i] is the Bitwise XOR of all the array elements of B[] except B[i]. Examples: Input: A[] = {13, 14, 10, 6}, K = 2Output: 2 1 5 9Explanatio
6 min read
Find K such that repeated subtraction of K from Array elements make the Array equal Given an array arr[] of size N, the task is to find the value of an integer K such that its repeated subtraction from array elements will make all array elements in minimum operations. Example: Input: arr[] = {5, 3, 3, 7}Output: 2Explanation: Minimum 2 operations must be performed:1st operation: sub
5 min read
Minimize adding odd and subtracting even numbers to make all array elements equal to K Given an array, arr[] of size N and an integer K, the task is to find the minimum number of operations required to make all array elements equal to K by performing the following operations any number of times: Convert arr[i] to arr[i] + X, where X is an odd number.Convert arr[i] to arr[i] - Y, where
7 min read
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other Given an array arr[] and an integer K, the task is to find the minimum number of operations to make all the elements of the array arr[] equal. In one operation, K is subtracted from an element and this K is added into other element. If it is not possible then print -1. Examples: Input: arr[] = {5, 8
7 min read
Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0 Given an array arr[] of N integers and a positive integer K, the task is to find the minimum number of operations required to reduce all array elements to 0 such that in each operation reduce any array element by 1 and independently at most K array element can be reduced to 0. Examples: Input: arr[]
6 min read
Rearrange given Array by replacing every element with the element located at mean of adjacent elements Given an array arr[]. The array contains numbers from 0 to N-1 only, where N is the size of arr[]. The task is to modify the array in such that for each i from 0â¤i<N, arr[i] = arr[ (arr[i-1] + arr[i+1]) / 2 ] There are a few exceptions: For first element of the array, arr[i-1] = 0; because previo
9 min read
Minimize maximum array element by splitting array elements into powers of two at most K times Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the maximum value of the array by splitting the array element into powers of 2 at most K times. Examples: Input: arr[] = {2, 4, 11, 2}, K = 2Output: 2Explanation:Below are the operations performed on arr
12 min read
Minimum increment and decrement by K of each pair elements required to make all array elements equal Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Cho
10 min read
Modify array by removing (arr[i] + arr[i + 1])th element exactly K times Given an array arr[] consisting of first N natural numbers, where arr[i] = i ( 1-based indexing ) and a positive integer K, the task is to print the array arr[] obtained after removing every (arr[i] + arr[i + 1])th element from the array in every ith operation exactly K times. Examples: Input: arr[]
9 min read