Check if a Zero Array can be converted to a given Array by incrementing some elements with X^i
Last Updated :
12 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int sod[100];
string solve(vector< int >& B, int N, int X)
{
for ( int i = 0; i < 100; i++) {
sod[i] = 0;
}
for ( int i = 0; i < N; i++) {
int t = 0;
while (B[i] != 0) {
sod[t] += B[i] % X;
++t;
B[i] /= X;
}
}
for ( int i = 0; i < 100; i++) {
if (sod[i] > 1) {
return "NO" ;
}
}
return "YES" ;
}
int main()
{
vector< int > B = { 0, 59059, 810 };
int N = 3, X = 9;
cout << solve(B, N, X);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int sod[] = new int [ 100 ];
static String solve( int [] B, int N, int X)
{
for ( int i = 0 ; i < 100 ; i++) {
sod[i] = 0 ;
}
for ( int i = 0 ; i < N; i++) {
int t = 0 ;
while (B[i] != 0 ) {
sod[t] += B[i] % X;
++t;
B[i] /= X;
}
}
for ( int i = 0 ; i < 100 ; i++) {
if (sod[i] > 1 ) {
return "NO" ;
}
}
return "YES" ;
}
public static void main(String[] args)
{
int B[] = { 0 , 59059 , 810 };
int N = 3 , X = 9 ;
System.out.println(solve(B, N, X));
}
}
|
Python
sod = []
def solve(B, N, X):
sod = [ 0 for i in range ( 100 )]
for i in range ( 0 , N):
t = 0
while (B[i] ! = 0 ):
sod[t] + = B[i] % X
t + = 1
B[i] / / = X
for i in range ( 0 , 100 ):
if (sod[i] > 1 ):
return "NO"
return "YES"
B = [ 0 , 59059 , 810 ]
N = 3
X = 9
print (solve(B, N, X))
|
C#
using System;
class GFG
{
static int []sod = new int [100];
static String solve( int [] B, int N, int X)
{
for ( int i = 0; i < 100; i++) {
sod[i] = 0;
}
for ( int i = 0; i < N; i++) {
int t = 0;
while (B[i] != 0) {
sod[t] += B[i] % X;
++t;
B[i] /= X;
}
}
for ( int i = 0; i < 100; i++) {
if (sod[i] > 1) {
return "NO" ;
}
}
return "YES" ;
}
public static void Main()
{
int []B = { 0, 59059, 810 };
int N = 3, X = 9;
Console.WriteLine(solve(B, N, X));
}
}
|
Javascript
<script>
let sod = new Array(100).fill(0);
const solve = (B, N, X) => {
for (let i = 0; i < 100; i++) {
sod[i] = 0;
}
for (let i = 0; i < N; i++) {
let t = 0;
while (B[i] != 0) {
sod[t] += B[i] % X;
++t;
B[i] = parseInt(B[i] / X);
}
}
for (let i = 0; i < 100; i++) {
if (sod[i] > 1) {
return "NO" ;
}
}
return "YES" ;
}
let B = [0, 59059, 810];
let N = 3, X = 9;
document.write(solve(B, N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...