📜  计算具有个位数整数和 K 的 Array 的子序列

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

计算具有个位数整数和 K 的 Array 的子序列

给定一个数组arr[]和整数K ,任务是计算数组的子序列数,以便在添加该子序列的所有元素后,它们的个位数整数和正好是K

注意:一位数整数和是通过将一个数字替换为其数字和直到该数字为一位数来获得的。例如,58 变为 13,然后是 4。4 是 53 的个位数之和。

例子:

方法:解决问题的想法是找到数组的所有子序列,并为每个子序列检查子序列和的个位数整数和是否为K。

请按照以下步骤解决问题:

  • 找到数组的所有子序列。
  • 对于每个子序列:
    • 求子序列的总和
    • 计算子序列和的个位数整数和(比如 X )。
    • 检查X是否等于K ,如果相等则增加计数
  • 返回最终计数作为所需答案。

下面是上述方法的实现。

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to replace integers
// with their digit sum value
int convert(int X)
{
    int sum = 0, temp = X;
 
    // Run a while till digit sum
    // become single integer
    while (temp > 9) {
        int temp2 = temp;
        while (temp2) {
 
            // Store the last digit
            int l = temp2 % 10;
            sum += l;
            temp2 = temp2 / 10;
        }
        temp = sum;
        sum = 0;
    }
 
    // Return digit sum
    return temp;
}
 
// Function to count subsequences
void countSubsequences(vector arr, int i,
                       vector s, int& count, int K)
{
 
    // If it is the leaf of
    // recursion tree
    if (i == arr.size()) {
 
        // Calculating subsequences
        // sum using stl
        int subSum = accumulate(s.begin(), s.end(), 0);
 
        // First convert subsequences sum
        // into single integer digit sum and
        // check if it equal to K or not if
        // it is then increment count
        if (convert(subSum) == K)
            count++;
    }
    else {
 
        // Subsequence without including
        // the element at current index
        countSubsequences(arr, i + 1, s,
                          count, K);
 
        // Push back current element
        s.push_back(arr[i]);
 
        // Subsequence including
        // the element at current index
        countSubsequences(arr, i + 1, s,
                          count, K);
    }
    return;
}
 
// Function to find all the subsequences
int solve(vector& arr, int K)
{
    int count = 0, i = 0;
    vector v;
 
    // Recursive function call
    countSubsequences(arr, i, v, count, K);
    return count;
}
 
// Driver code
int main()
{
    vector arr = { 9, 8, 6, 10 };
    int K = 6;
 
    int count = solve(arr, K);
    cout << count;
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to replace integers
  // with their digit sum value
  public static int convert(int X)
  {
    int sum = 0, temp = X;
 
    // Run a while till digit sum
    // become single integer
    while (temp > 9) {
      int temp2 = temp;
      while (temp2 > 0) {
 
        // Store the last digit
        int l = temp2 % 10;
        sum += l;
        temp2 = temp2 / 10;
      }
      temp = sum;
      sum = 0;
    }
 
    // Return digit sum
    return temp;
  }
 
  // Function to count subsequences
  public static void
    countSubsequences(int arr[], int i,
                      ArrayList s, int count[],
                      int K)
  {
 
    // If it is the leaf of
    // recursion tree
    if (i == arr.length) {
 
      // Calculating subsequences
      // sum using stl
      int subSum = 0;
      for (int j = 0; j < s.size(); j++)
        subSum += s.get(j);
 
      // First convert subsequences sum
      // into single integer digit sum and
      // check if it equal to K or not if
      // it is then increment count
      if (convert(subSum) == K)
        count[0]++;
    }
    else {
 
      // Subsequence without including
      // the element at current index
      countSubsequences(arr, i + 1, s, count, K);
 
      // Push back current element
      s.add(arr[i]);
 
      // Subsequence including
      // the element at current index
      countSubsequences(arr, i + 1, s, count, K);
      s.remove(s.size()-1);   
    }
    return;
  }
  // Function to find all the subsequences
  public static int[] solve(int arr[], int K)
  {
    int count[] = {0};
    int i = 0;
    ArrayList list = new ArrayList<>();
 
    // Recursive function call
    countSubsequences(arr, i, list, count, K);
    return count;
  }
 
  public static void main(String[] args)
  {
    int arr[] = { 9, 8, 6, 10 };
    int K = 6;
 
    int count[] = solve(arr, K);
    System.out.print(count[0]);
  }
}
 
// This code is contributed by Rohit Pradhan


Python3
# Python3 code to implement the above approach
 
# function to convert integer
# to its digit sum value
def convert(X):
    sums = 0
    temp = X
    while temp > 9:
        temp2 = temp
        while temp2 > 0:
            sums += temp2 % 10
            temp2 //= 10
        temp = sums
        sums = 0
    return temp
 
# function to count subsequence
def countSubsequences(arr, i, s, K):
    global count
    if i == len(arr):
        if convert(sum(s)) == K:
            count += 1
    else:
        countSubsequences(arr, i + 1, s, K)
        countSubsequences(arr, i + 1, s + [arr[i]], K)
    return
 
def solve(K):
    global count
    count = 0
    i = 0
    v = []
    countSubsequences(arr, 0, [], K)
    return count
 
# Driver Code
arr = [9, 8, 6, 10]
K = 6
print(solve(K))
 
# This code is contributed by phasing17.


Javascript


输出
4

时间复杂度: O( 2 N * N)
辅助空间: O(N)