📌  相关文章
📜  从 1 到 N 作为 K 因子的 M 最大不同数字和的总和

📅  最后修改于: 2021-09-04 08:11:01             🧑  作者: Mango

给定一个最大为N的自然数数组和两个数MK ,任务是从N 个自然数中找到 M 个最大不同数字和M数的和,这些数是K 的因数。

例子:

朴素的方法:简单的解决方案是运行从 1 到 N 的循环并找到所有能完美整除 K 的数字的列表。找到每个因子的数字和并按降序对它们进行排序,然后从该列表中从顶部打印 M 个不同的元素。

有效的方法:

  • 通过从 2 到 √K 迭代,找出 1 到 N 范围内 K 的所有因数,使得元素完全整除数字。有关此方法的详细说明,请参阅本文并将它们存储在数组中。
  • 找出存储在 factor 数组中的数字的数字和。
    例如:
For the Given Array - {4, 10, 273 }

Digit Sum of the Elements - 
Element 0 Digit Sum - "4" = 4
Element 1 Digit Sum - "10" = 1 + 0 = 10
Element 2 Digit Sum - "273" = 2 + 7 + 3 = 12
  • 从数字总和中删除重复项,因为我们需要不同的元素。
  • 以相反的顺序对不同的数字和进行排序,并从这个数字和数组中找到第一个至多 M 个元素的总和。

下面是上述方法的实现。

C++
// C++ implementation to find the sum of
// maximum distinct digit sum of at most
// M numbers from 1 to N that are factors of K
 
#include 
using namespace std;
 
// Function to find the factors
// of K in N
vector findFactors(int n, int k)
{
 
    // Initialise a vector
    vector factors;
 
    // Find out the factors of
    // K less than N
    for (int i = 1; i <= sqrt(k); i++) {
        if (k % i == 0) {
            if (k / i == i && i <= n)
                factors.push_back(i);
            else {
                if (i <= n)
                    factors.push_back(i);
                if (k / i <= n)
                    factors.push_back(k / i);
            }
        }
    }
 
    return factors;
}
 
// Find the digit sum of each factor
vector findDigitSum(vector a)
{
 
    // Sum of digits for each
    // element in vector
    for (int i = 0; i < a.size(); i++) {
        int c = 0;
        while (a[i] > 0) {
 
            c += a[i] % 10;
            a[i] = a[i] / 10;
        }
        a[i] = c;
    }
 
    return a;
}
 
// Find the largest M distinct digit
// sum from the digitSum vector
int findMMaxDistinctDigitSum(
    vector distinctDigitSum,
    int m)
{
    // Find the sum of last M numbers.
    int sum = 0;
    for (int i = distinctDigitSum.size() - 1;
         i >= 0 && m > 0;
         i--, m--)
        sum += distinctDigitSum[i];
 
    return sum;
}
 
// Find the at most M numbers from N natural
// numbers whose digit sum is distinct
// and those M numbers are factors of K.
int findDistinctMnumbers(int n, int k, int m)
{
 
    // Find out the factors of
    // K less than N
    vector factors = findFactors(n, k);
 
    // Sum of digits for each
    // element in vector
    vector digitSum = findDigitSum(factors);
 
    // Sorting the digitSum vector
    sort(digitSum.begin(), digitSum.end());
 
    // Removing the duplicate elements
    vector::iterator ip;
    ip = unique(digitSum.begin(), digitSum.end());
 
    digitSum.resize(distance(
        digitSum.begin(),
        ip));
 
    // Finding the sum and returning it
    return findMMaxDistinctDigitSum(digitSum, m);
}
 
// Driver Code
int main()
{
    int n = 100, k = 80, m = 4;
 
    // Function Call
    cout
        << findDistinctMnumbers(n, k, m)
        << endl;
 
    return 0;
}


Java
// Java implementation to find the sum of
// maximum distinct digit sum of at most
// M numbers from 1 to N that are factors of K
import java.util.*;
 
class GFG
{
 
// Function to find the factors
// of K in N
public static Vector findFactors(int n, int k)
{
 
    Vector factors = new Vector();
    // Initialise a vector
 
    // Find out the factors of
    // K less than N
    for (int i = 1; i <= Math.sqrt(k); i++)
    {
        if (k % i == 0)
        {
            if (k / i == i && i <= n)
                factors.add(i);
            else
            {
                if (i <= n)
                    factors.add(i);
                if (k / i <= n)
                    factors.add(k / i);
            }
        }
    }
 
    return factors;
}
 
// Find the digit sum of each factor
public static Vector findDigitSum(Vector a)
{
 
    // Sum of digits for each
    // element in vector
    for (int i = 0; i < a.size(); i++)
    {
        int c = 0;
        while (a.get(i) > 0)
        {
 
            c += (a.get(i) % 10);
            a.set(i,(a.get(i)/10));
        }
        a.set(i,c);
    }
 
    return a;
}
 
// Find the largest M distinct digit
// sum from the digitSum vector
public static int findMMaxDistinctDigitSum(Vector distinctDigitSum,int m)
{
    // Find the sum of last M numbers.
    int sum = 0;
    for (int i = distinctDigitSum.size() - 1;
            i >= 0 && m > 0;i--, m--)
        sum += distinctDigitSum.get(i);
 
    return sum;
}
 
// Find the at most M numbers from N natural
// numbers whose digit sum is distinct
// and those M numbers are factors of K.
public static int findDistinctMnumbers(int n, int k, int m)
{
 
    // Find out the factors of
    // K less than N
    Vector factors = findFactors(n, k);
 
    // Sum of digits for each
    // element in vector
    Vector digitSum = findDigitSum(factors);
 
    // Sorting the digitSum vector
     
    Collections.sort(digitSum);
 
    // Removing the duplicate elements
 
    HashSet hs1 = new HashSet(digitSum);
 
    //"HashSet" is stores only unique elements
 
    Vector vect2 = new Vector(hs1);
 
    // Finding the sum and returning it
    return findMMaxDistinctDigitSum(vect2, m);
}
 
// Driver Code
public static void main(String args[])
{
    int n = 100, k = 80, m = 4;
 
    // Function Call
    System.out.println(findDistinctMnumbers(n, k, m));
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python 3 implementation to find the sum of
# maximum distinct digit sum of at most
# M numbers from 1 to N that are factors of K
import math
 
# Function to find the factors
# of K in N
def findFactors(n, k):
 
    # Initialise a vector
    factors = []
 
    # Find out the factors of
    # K less than N
    sqt = (int)(math.sqrt(k))
    for i in range(1, sqt):
        if (k % i == 0):
            if (k // i == i and i <= n):
                factors.append(i)
            else:
                if (i <= n):
                    factors.append(i)
                if (k // i <= n):
                    factors.append(k // i)
    return factors
 
# Find the digit sum of each factor
def findDigitSum(a):
 
    # Sum of digits for each
    # element in vector
    for i in range(len(a)):
        c = 0
        while (a[i] > 0):
            c += a[i] % 10
            a[i] = a[i] // 10
        a[i] = c
    return a
 
# Find the largest M distinct digit
# sum from the digitSum vector
def findMMaxDistinctDigitSum(
        distinctDigitSum, m):
 
    # Find the sum of last M numbers.
    sum = 0
    i = len(distinctDigitSum) - 1
    while (i >= 0 and m > 0):
        sum += distinctDigitSum[i]
        i -= 1
        m -= 1
    return sum
 
# Find the at most M numbers from N natural
# numbers whose digit sum is distinct
# and those M numbers are factors of K
def findDistinctMnumbers(n, k, m):
 
    # Find out the factors of
    # K less than N
    factors = findFactors(n, k)
 
    # Sum of digits for each
    # element in vector
    digitSum = findDigitSum(factors)
 
    # Sorting the digitSum vector
    digitSum.sort()
 
    # Removing the duplicate elements
    ip = list(set(digitSum))
 
    # Finding the sum and returning it
    return findMMaxDistinctDigitSum(ip, m)
 
# Driver Code
if __name__ == "__main__":
 
    n = 100
    k = 80
    m = 4
 
    # Function Call
    print(findDistinctMnumbers(n, k, m))
 
    # This code is contributed by chitranayal


Javascript


输出:
24

性能分析:

  • 时间复杂度:在给定的方法中,主要有以下两个过程:
    • 求一个数的因数的时间复杂度为:O(√(K))
    • 排序和存储唯一元素的时间复杂度:O(√(K)log(√(K)))
  • 辅助空间:在给定的方法中,有一个额外的数组用于存储数字 K 的因子,即: O(√(K))

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