Print all distinct integers that can be formed by K numbers from a given array of N numbers
Given an array of N elements and an integer K, print all the distinct integers which can be formed by choosing K numbers from the given N numbers. A number from an array can be chosen any number of times.
Examples:
Input: k = 2, a[] = {3, 8, 17, 5}
Output: The 10 distinct integers are:
6 8 10 11 13 16 20 22 25 34
The 2 elements chosen are:
3+3 = 6, 5+3 = 8, 5+5 = 10, 8+3 = 11, 8+5 = 13
8+8 = 16, 17+3 = 20, 17+5 = 22, 17+8 = 25, 17+17 = 34.
Input: k = 3, a[] = {3, 8, 17}
Output: The 10 distinct integers are:
9 14 19 23 24 28 33 37 42 51
Approach: The problem will be solved using recursion. All combinations are to be tried, when the count of elements selected is equal to k, then we keep the num formed in the set so that the repetitive elements are not counted twice. The function generateNumber(int count, int a[], int n, int num, int k) is a recursive function, in which the base case is when the count becomes K which signifies that K elements from the array have been chosen. num in the parameter signifies the number formed by count number of numbers. In the function, iterate over the array and for every element, call the recursive function with count as count+1 and num as num+a[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
set< int > s;
void generateNumber( int count, int a[], int n,
int num, int k)
{
if (k == count) {
s.insert(num);
return ;
}
for ( int i = 0; i < n; i++) {
generateNumber(count + 1, a, n, num + a[i], k);
}
}
void printDistinctIntegers( int k, int a[], int n)
{
generateNumber(0, a, n, 0, k);
cout << "The " << s.size()
<< " distinct integers are:\n" ;
while (!s.empty()) {
cout << *s.begin() << " " ;
s.erase(*s.begin());
}
}
int main()
{
int a[] = { 3, 8, 17, 5 };
int n = sizeof (a) / sizeof (a[0]);
int k = 2;
printDistinctIntegers(k, a, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static TreeSet<Integer> set =
new TreeSet<Integer>();
public static void generateNumber( int count, int a[],
int n, int num, int k)
{
if (count == k)
{
set.add(num);
return ;
}
for ( int i = 0 ; i < n; i++)
generateNumber(count + 1 , a, n,
num + a[i], k);
}
public static void printDistinctIntegers( int k,
int a[], int n)
{
generateNumber( 0 , a, n, 0 , k);
System.out.print( "The" + " " + set.size() +
" " + "distinct integers are: " );
System.out.println();
Iterator<Integer> i = set.iterator();
while (set.isEmpty() == false )
{
while (i.hasNext())
{
System.out.print(i.next() + " " );
}
}
}
public static void main (String[] args)
{
int arr[] = { 3 , 8 , 17 , 5 };
int n = arr.length;
int k = 2 ;
printDistinctIntegers(k, arr, n);
}
}
|
Python3
s = set ()
def generateNumber(count, a, n, num, k):
if k = = count:
s.add(num)
return
for i in range ( 0 , n):
generateNumber(count + 1 , a, n,
num + a[i], k)
def printDistinctIntegers(k, a, n):
generateNumber( 0 , a, n, 0 , k)
print ( "The" , len (s),
"distinct integers are:" )
for i in sorted (s):
print (i, end = " " )
if __name__ = = "__main__" :
a = [ 3 , 8 , 17 , 5 ]
n, k = len (a), 2
printDistinctIntegers(k, a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet< int > set =
new SortedSet< int >();
public static void generateNumber( int count, int []a,
int n, int num, int k)
{
if (count == k)
{
set .Add(num);
return ;
}
for ( int i = 0; i < n; i++)
generateNumber(count + 1, a, n,
num + a[i], k);
}
public static void printDistinctIntegers( int k,
int []a, int n)
{
generateNumber(0, a, n, 0, k);
Console.Write( "The" + " " + set .Count +
" " + "distinct integers are: " );
Console.WriteLine();
foreach ( int sets in set )
{
Console.Write(sets + " " );
}
}
public static void Main (String[] args)
{
int []arr = {3, 8, 17, 5};
int n = arr.Length;
int k = 2;
printDistinctIntegers(k, arr, n);
}
}
|
Javascript
<script>
var s = new Set();
function generateNumber(count, a, n, num, k)
{
if (k == count)
{
s.add(num);
return ;
}
for ( var i = 0; i < n; i++)
{
generateNumber(count + 1, a, n,
num + a[i], k);
}
}
function printDistinctIntegers(k, a, n)
{
generateNumber(0, a, n, 0, k);
document.write( "The " + s.size +
" distinct integers are:<br>" );
while (s.size != 0)
{
var tmp = [...s].sort((a, b) => a - b)[0]
document.write(tmp + " " );
s. delete (tmp);
}
}
var a = [ 3, 8, 17, 5 ];
var n = a.length;
var k = 2;
printDistinctIntegers(k, a, n);
</script>
|
Output
The 10 distinct integers are:
6 8 10 11 13 16 20 22 25 34
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...