📌  相关文章
📜  通过在每个第 i 步中从数组元素中减去 K^i 使所有数组元素等于 0 来修改数组

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

通过在每个第 i 步中从数组元素中减去 K^i 使所有数组元素等于 0 来修改数组

给定一个大小为N的数组arr[] ,任务是在第i步通过从数组元素中减去K i来检查是否可以将所有数组元素转换为0 s。如果可以这样做,请打印“”。否则,打印“”。

例子:

解决方法:按照以下步骤解决问题:

  • 初始化一个向量,比如V,以存储K的所有可能的幂。
  • 此外,初始化一个 map,比如MP ,以存储是否使用了K的幂。
  • 初始化一个变量,比如X1,以存储K的幂数。
  • 迭代直到X 小于INT_MAX并执行以下步骤:
    • X的值推入向量中。  
    • X乘以K。
  • 使用变量i遍历范围[0, N – 1]并执行以下步骤:  
    • 使用变量j反向迭代向量V ,并执行以下步骤:
      • 如果arr[i]大于V[j]并且MP[V[j]]0,则从arr[i]中减去V[j ]。
      • MP[V[j]]更新为1
    • 如果arr[i]不等于0 ,则从循环中中断。
  • 如果i小于N,则打印“No”,因为数组元素不能为0 。否则,打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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 MP;
 
    // Stores all the Kth power
    vector 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 MP = new HashMap();
 
        // Stores all the Kth power
        ArrayList V = new ArrayList();
 
        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 MP = new Dictionary();
 
        // Stores all the Kth power
        List V = new List();
 
        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


输出:
Yes

时间复杂度: O(N* log K (INT_MAX))
辅助空间: O(log K (INT_MAX))