📌  相关文章
📜  检查是否可以通过使用 X^i 递增一些元素来将零数组转换为给定数组

📅  最后修改于: 2022-05-13 01:56:04.975000             🧑  作者: Mango

检查是否可以通过使用 X^i 递增一些元素来将零数组转换为给定数组

给定一个数组B和两个整数N, X (2 ≤ X ≤ 100) 。最初,数组arr的所有值都为零。任务是在执行给定操作后检查数组arr[]是否可以等于给定数组B

  • 选择给定位置(p) (1 ≤ p ≤ n)
  • 然后将arr[p]增加X i其中i是任何正整数。
  • 任何元素都可以选择增加任意次数(可能是 0 次)。

例子:

方法:由于问题是处理 X 的幂,首先要做的是对数组 B 中的每个元素,在基数 X 中找到它的表示

  • 当 i = 0 时,只选择某个位置并将其增加 X 0 ,这意味着在数组 B 中的所有元素中,只有一个元素可以在基数 X 中具有个位,并且等于 1。
  • 类似地,对于更多的数字,因为已经用个位执行了一次操作,然后继续到十位甚至更远。
  • 因此,如果任何位置的数字总和大于 1,则答案将为“否”,因为每个位置只能增加 1。否则答案将为“是”。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Array to store sum
// Of value of digits
int sod[100];
 
// Function to do required operations
string solve(vector& 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 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



输出:
YES

时间复杂度: O(N * log X (a)),其中 a 是数组 B 中的最大元素。
辅助空间: O(1)