📌  相关文章
📜  计算不同数字总和的最大和小于或等于M的数字

📅  最后修改于: 2021-05-06 20:35:48             🧑  作者: Mango

给定一个整数数组arr []和一个数字M ,任务是查找其不同位数和之和小于或等于给定数字M的数字的最大计数。

例子:

方法:
这个想法是找到数组中每个元素的数字总和,然后对数字总和数组进行排序。

现在问题可以归结为对排序后的不同数字总和数组中的元素数进行计数,总和小于或等于M。

为此,请使用最小的不同数字总和,直到这些数字的总和小于或等于给定数字M,然后返回这些数字的计数。

举例说明:

Given Array be - arr[] = {1, 45, 17, 32, 22}, M = 10

Then Digit-sum of each number in the array - 
Digit-sum(1) = 1
Digit-sum(45) = 4 + 5 = 9
Digit-sum(17) = 1 + 7 = 8
Digit-sum(32) = 3 + 2 = 5
Digit-sum(22) = 2 + 2 = 4

After sorting the digit-sum array - 
Digit-sum[] = {1, 4, 5, 8, 9}

Then there are three numbers such that, 
there sum is less than or equal to M = 10
which is {1, 4, 5} 
Sum = 1 + 4 + 5 = 10 ≤ M

算法:

  • 查找数组中每个元素的数字和,并将其存储在另一个数组中(例如digit-sum [] )
  • 以递增顺序对digit-sum []数组进行排序。
  • 从已排序的digit-sum []数组中删除重复的元素,以使仅存在不正确的digit-sum。
  • 将变量sum初始化为0以存储当前和。
  • 迭代数位总和[]数组和元素添加到总和直到总和的值小于等于M和递增计数。

下面是上述方法的实现:

C++
// C++ implementation to find the 
// Maximum count of numbers whose
// sum of distinct digit-sum less
// than or equal to the given number
  
#include 
using namespace std;
  
// Function to find the 
// digit-sum of a number 
int SumofDigits(int digit)
{
    int sum = 0;
      
    // Loop to iterate the number
    // digit-wise to find digit-sum
    while (digit != 0) {
          
        // variable to store last digit
        int rem = digit % 10;
        sum += rem;
        digit /= 10;
    }
    return sum;
}
  
// Function to find the count of number
int findCountofNumbers(int arr[], 
                   int n, int M){
      
    // Vector to store the Sum of Digits
    vector SumDigits;
  
    // Sum of digits for each
    // element in vector
    for (int i = 0; i < n; i++) {
        int s = SumofDigits(arr[i]);
        SumDigits.push_back(s);
    }
  
    // Sorting the digitSum vector
    sort(SumDigits.begin(), SumDigits.end());
  
    // Removing the duplicate elements
    vector::iterator ip;
    ip = unique(SumDigits.begin(), 
                 SumDigits.end());
    SumDigits.resize(distance(
         SumDigits.begin(), ip)
         );
  
    // Count variable to store the Count
    int count = 0;
    int sum = 0;
    // Finding the Count of Numbers
    for (int i = 0; i < SumDigits.size(); i++) {
        if (sum > M)
            break;
        sum += SumDigits[i];
        if (sum <= M)
            count++;
    }
    return count;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 1, 45, 16, 17, 
           219, 32, 22 }, M = 10;
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << findCountofNumbers(arr, n, M);
    return 0;
}


Python3
# Python 3 implementation to find the 
# Maximum count of numbers whose
# sum of distinct digit-sum less
# than or equal to the given number
  
# Function to find the 
# digit-sum of a number 
def SumofDigits( digit):
      
    sum = 0
      
    # Loop to iterate the number
    # digit-wise to find digit-sum
    while (digit != 0):
          
        # variable to store last digit
        rem = digit % 10
        sum += rem
        digit //= 10
      
    return sum
  
# Function to find the count of number
def findCountofNumbers(arr, n, M):
      
    # Vector to store the Sum of Digits
    SumDigits = []
  
    # Sum of digits for each
    # element in vector
    for i in range( n ):
        s = SumofDigits(arr[i])
        SumDigits.append(s)
  
    # Sorting the digitSum vector
    SumDigits.sort()
  
    # Removing the duplicate elements
    ip = list(set(SumDigits))
  
    # Count variable to store the Count
    count = 0
    sum = 0
      
    # Finding the Count of Numbers
    for i in range(len(SumDigits)):
        if (sum > M):
            break
        sum += SumDigits[i]
        if (sum <= M):
            count+=1
      
    return count
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 1, 45, 16, 17, 
        219, 32, 22 ]
    M = 10
    n = len(arr)
  
    # Function Call
    print( findCountofNumbers(arr, n, M))
      
# This ccode is contributed by chitranayal


输出:
3

性能分析:

  • 时间复杂度:与给定方法一样,我们使用的排序方式是在最坏的情况下采用O(NlogN),而要找到每个元素的数字总和则采用O(N * K),其中K是最大位数,因此,时间复杂度将为O(NlogN + N * k)
  • 辅助空间: O(N)