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

📅  最后修改于: 2021-09-06 06:18:47             🧑  作者: 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[] 数组中删除重复元素,以便只有 unqiue 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


Javascript


输出:
3

性能分析:

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live