Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 21 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;
}

Java




// Java code for the above approach
import java.io.*;
class GFG
{
 
  // Array to store sum
  // Of value of digits
  static int sod[] = new int[100];
 
  // Function to do required operations
  static String solve(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";
  }
  public static void main(String[] args)
  {
    int B[] = { 0, 59059, 810 };
    int N = 3, X = 9;
 
    // Function call
    System.out.println(solve(B, N, X));
  }
}
 
// This code is contributed by Potta Lokesh

Python




# Python code to implement the above approach
 
# Array to store sum
# Of value of digits
sod = []
 
# Function to do required operations
def solve(B, N, X):
 
    # Making values in digits array to zero
    sod = [0 for i in range(100)]
     
    for i in range(0, N):
        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 += 1
            B[i] //= X
 
    for i in range(0, 100):
 
        # If at any point
        # Digits array element become
        # Greater than 1, ans = "NO"
        if (sod[i] > 1):
            return "NO"
    return "YES"
 
# Driver Code
 
B = [ 0, 59059, 810 ]
N = 3
X = 9
 
# Function call
print(solve(B, N, X))
 
# This code is contributed by Samim Hossain Mondal.

C#




// C# code for the above approach
using System;
 
class GFG
{
 
  // Array to store sum
  // Of value of digits
  static int []sod = new int[100];
 
  // Function to do required operations
  static String solve(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";
  }
  public static void Main()
  {
    int []B = { 0, 59059, 810 };
    int N = 3, X = 9;
 
    // Function call
    Console.WriteLine(solve(B, N, X));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
    // JavaScript code to implement the above approach
 
    // Array to store sum
    // Of value of digits
    let sod = new Array(100).fill(0);
 
    // Function to do required operations
    const solve = (B, N, X) => {
 
        // Making values in digits array to zero
        for (let i = 0; i < 100; i++) {
            sod[i] = 0;
        }
        for (let i = 0; i < N; i++) {
            let 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] = parseInt(B[i] / X);
            }
        }
 
        for (let 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
 
    let B = [0, 59059, 810];
    let N = 3, X = 9;
 
    // Function call
    document.write(solve(B, N, X));
 
    // This code is contributed by rakeshsahni
 
</script>

 
 

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!