Sum of M maximum distinct digit sum from 1 to N that are factors of K
Given an array of natural numbers upto N and two numbers M and K, the task is to find the sum of M maximum distinct digit sum M numbers from N natural numbers which are factors of K.
Examples:
Input: N = 50, M = 4, K = 30
Output: 16
Explanation:
From 1 to 50, factors of 30 = {1, 2, 3, 5, 6, 10, 15, 30}.
Digit sum of every factor = {1, 2, 3, 5, 6, 1, 6, 3}.
4 largest digit sum = 2, 3, 5, 6.
Sum = 16
Input: N = 5, M = 3, K = 74
Output: 3
Explanation:
From 1 to 5 factors of 74 = {1, 2}
Digit sum of every factor = {1, 2}.
3 largest digit sum = 1, 2 (Here only 2 such numbers are present. But it is asked for atmost M. So these 2 will be considered only.)
Sum = 3
Naive Approach: Simple solution is to run the loop from 1 to N and find the list of all numbers that perfectly divides K. Find the digit sum of each factor and sort them in descending order and print M distinct elements from this list from top.
Efficient Approach:
- Find all the factors of K in the range of 1 to N by iterating from 2 to ?K such that the element completely divides the number. For Detailed Explanation of this approach please refer this article and store them in an array.
- Find the digit sum of the numbers stored in the factors array.
For Example:
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
- Remove duplicates from the digit sum as we need distinct elements.
- Sort the distinct digit sums in reverse order and find the sum of first at most M elements out of this digit-sum array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findFactors( int n, int k)
{
vector< int > factors;
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;
}
vector< int > findDigitSum(vector< int > a)
{
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;
}
int findMMaxDistinctDigitSum(
vector< int > distinctDigitSum,
int m)
{
int sum = 0;
for ( int i = distinctDigitSum.size() - 1;
i >= 0 && m > 0;
i--, m--)
sum += distinctDigitSum[i];
return sum;
}
int findDistinctMnumbers( int n, int k, int m)
{
vector< int > factors = findFactors(n, k);
vector< int > digitSum = findDigitSum(factors);
sort(digitSum.begin(), digitSum.end());
vector< int >::iterator ip;
ip = unique(digitSum.begin(), digitSum.end());
digitSum.resize(distance(
digitSum.begin(),
ip));
return findMMaxDistinctDigitSum(digitSum, m);
}
int main()
{
int n = 100, k = 80, m = 4;
cout
<< findDistinctMnumbers(n, k, m)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static Vector<Integer> findFactors( int n, int k)
{
Vector<Integer> factors = new Vector<Integer>();
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;
}
public static Vector<Integer> findDigitSum(Vector<Integer> a)
{
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;
}
public static int findMMaxDistinctDigitSum(Vector<Integer> distinctDigitSum, int m)
{
int sum = 0 ;
for ( int i = distinctDigitSum.size() - 1 ;
i >= 0 && m > 0 ;i--, m--)
sum += distinctDigitSum.get(i);
return sum;
}
public static int findDistinctMnumbers( int n, int k, int m)
{
Vector<Integer> factors = findFactors(n, k);
Vector<Integer> digitSum = findDigitSum(factors);
Collections.sort(digitSum);
HashSet<Integer> hs1 = new HashSet<Integer>(digitSum);
Vector<Integer> vect2 = new Vector<Integer>(hs1);
return findMMaxDistinctDigitSum(vect2, m);
}
public static void main(String args[])
{
int n = 100 , k = 80 , m = 4 ;
System.out.println(findDistinctMnumbers(n, k, m));
}
}
|
Python3
import math
def findFactors(n, k):
factors = []
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
def findDigitSum(a):
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
def findMMaxDistinctDigitSum(
distinctDigitSum, m):
sum = 0
i = len (distinctDigitSum) - 1
while (i > = 0 and m > 0 ):
sum + = distinctDigitSum[i]
i - = 1
m - = 1
return sum
def findDistinctMnumbers(n, k, m):
factors = findFactors(n, k)
digitSum = findDigitSum(factors)
digitSum.sort()
ip = list ( set (digitSum))
return findMMaxDistinctDigitSum(ip, m)
if __name__ = = "__main__" :
n = 100
k = 80
m = 4
print (findDistinctMnumbers(n, k, m))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static List< int > findFactors( int n, int k)
{
var factors = new List< int >();
for ( int i = 1; i <= Math.Sqrt(k); i++)
{
if (k % i == 0)
{
if (( int )(k / i) == i && i <= n)
{
factors.Add(i);
}
else
{
if (i <= n)
{
factors.Add(i);
}
if (( int )(k / i) <= n)
{
factors.Add(( int )(k / i));
}
}
}
}
return factors;
}
public static List< int > findDigitSum(List< int > a)
{
for ( int i = 0; i < a.Count; i++)
{
var c = 0;
while (a[i] > 0)
{
c += (a[i] % 10);
a[i] = (( int )(a[i] / 10));
}
a[i] = c;
}
return a;
}
public static int findMMaxDistinctDigitSum(List< int > distinctDigitSum, int m)
{
var sum = 0;
for ( int i = distinctDigitSum.Count - 1; i >= 0 && m > 0; i--, m--)
{
sum += distinctDigitSum[i];
}
return sum;
}
public static int findDistinctMnumbers( int n, int k, int m)
{
var factors = GFG.findFactors(n, k);
var digitSum = GFG.findDigitSum(factors);
digitSum.Sort();
var hs1 = new HashSet< int >(digitSum);
var vect2 = new List< int >(hs1);
return GFG.findMMaxDistinctDigitSum(vect2, m);
}
public static void Main(String[] args)
{
var n = 100;
var k = 80;
var m = 4;
Console.WriteLine(GFG.findDistinctMnumbers(n, k, m));
}
}
|
Javascript
<script>
function findFactors(n, k)
{
let factors = [];
for (let i = 1; i <= Math.sqrt(k); i++)
{
if (k % i == 0)
{
if (k / i == i && i <= n)
factors.push(i);
else
{
if (i <= n)
factors.push(i);
if (k / i <= n)
factors.push(k / i);
}
}
}
return factors;
}
function findDigitSum(a)
{
for (let i = 0; i < a.length; i++)
{
let c = 0;
while (a[i] > 0)
{
c += (a[i] % 10);
a[i] = Math.floor(a[i] / 10);
}
a[i] = c;
}
return a;
}
function findMMaxDistinctDigitSum(distinctDigitSum, m)
{
let sum = 0;
for (let i = distinctDigitSum.length - 1;
i >= 0 && m > 0;i--, m--)
sum += distinctDigitSum[i];
return sum;
}
function findDistinctMnumbers(n, k, m)
{
let factors = findFactors(n, k);
let digitSum = findDigitSum(factors);
digitSum.sort( function (a, b){ return a - b;});
let hs1 = new Set(digitSum);
let vect2 = Array.from(hs1);
return findMMaxDistinctDigitSum(vect2, m);
}
let n = 100, k = 80, m = 4;
document.write(findDistinctMnumbers(n, k, m));
</script>
|
Performance Analysis:
- Time Complexity: In the given approach, there are mainly two process which is as follows –
- Time Complexity to find the factors of a number is: O(?(K))
- Time complexity to sort and store the unique elements: O(?(K)log(?(K)))
- Auxiliary Space: In the given approach there is an extra array used to store the factors of the number K, which is: O(?(K))
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...