The Wayback Machine - https://web.archive.org/web/20220215143421/https://www.geeksforgeeks.org/check-if-a-zero-array-can-be-converted-to-a-given-array-by-incrementing-some-elements-with-xi/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if a Zero Array can be converted to a given Array by incrementing some elements with X^i

  • Last Updated : 15 Feb, 2022

Given an array B, and two integers N, X (2 ≤ X ≤ 100). Initially, array arr has all its values as zero. The task is to check if array arr[] can be made equal to the given array B after performing the given operations:

  • Choose a given position (p) (1 ≤ p ≤ n)
  • then increase arr[p] by Xi where i is any positive integer.
  • Any element can be chosen to be incremented any number of times (maybe 0 times).

Examples:

Input: N = 3, X = 9
B[] = {0, 59059, 810}
Output: YES
Explanation: Initially all values in arr[] are  0. arr[] = {0, 0, 0}
Choose arr[3], and add 92and 93 in two consecutive operations making it equal to 810. (arr[] = {0, 0, 810}).
Choose arr[2] and add 95 to it making it 59059.
Thus now the entire array arr = {0, 59059, 810} which is equal to array b. 

Input: N = 2, X = 10
B[] = {0, 0}
Output: YES
Explanation: The array initially is in the given stage. No need to increment any value.

 

Approach: As the question is dealing with the power of X, the first thing to do is for each element in the array B, find its representation in base X. 

  • When i = 0, then only choose some position and increase it with X0, which means in all of the elements in array B only one can have units digit in base X and that would be equal to 1.
  • Similarly for further digits, as operations have been performed with units digit once, then move on to tens digit and further.
  • Thus, if any position with the sum of digits greater than 1, the answer will be “NO” as each position can only be incremented by 1. else answer will be “YES”.

Below is the implementation of the above approach:

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Array to store sum
// Of value of digits
int sod[100];
  
// Function to do required operations
string solve(vector<int>& B, int N, int X)
{
  
    // Making values in digits array to zero
    for (int i = 0; i < 100; i++) {
        sod[i] = 0;
    }
    for (int i = 0; i < N; i++) {
        int t = 0;
  
        // ]Checking till number is
        // Greater than zero and
        // Calculating digits of a number
        // In base x
        while (B[i] != 0) {
  
            // Adding to t'th digit of b
            sod[t] += B[i] % X;
            ++t;
            B[i] /= X;
        }
    }
  
    for (int i = 0; i < 100; i++) {
  
        // If at any point
        // Digits array element become
        // Greater than 1, ans = "NO"
        if (sod[i] > 1) {
            return "NO";
        }
    }
    return "YES";
}
  
// Driver Code
int main()
{
    vector<int> B = { 0, 59059, 810 };
    int N = 3, X = 9;
  
    // Function call
    cout << solve(B, N, X);
    return 0;
}
Output:
YES

Time Complexity: O(N * logX(a)), where a is the maximum element in array B.
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!