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++
// C++ program to print all distinct // integers that can be formed by K numbers // from a given array of N numbers. #include <bits/stdc++.h> using namespace std; // stores all the distinct integers formed set<int> s; // Function to generate all possible numbers void generateNumber(int count, int a[], int n, int num, int k) { // Base case when K elements // are chosen if (k == count) { // insert it in set s.insert(num); return; } // Choose every element and call the function for (int i = 0; i < n; i++) { generateNumber(count + 1, a, n, num + a[i], k); } } // Function to print the distinct integers void printDistinctIntegers(int k, int a[], int n) { generateNumber(0, a, n, 0, k); cout << "The " << s.size() << " distinct integers are:\n"; // prints all the elements in the set while (!s.empty()) { cout << *s.begin() << " "; // erase the number after printing it s.erase(*s.begin()); } } // Driver Code int main() { int a[] = { 3, 8, 17, 5 }; int n = sizeof(a) / sizeof(a[0]); int k = 2; // Calling Function printDistinctIntegers(k, a, n); return 0; } |
Java
// Java program to print all // distinct integers that can // be formed by K numbers from // a given array of N numbers. import java.util.*; import java.lang.*; class GFG { // stores all the distinct // integers formed static TreeSet<Integer> set = new TreeSet<Integer>(); // Function to generate // all possible numbers public static void generateNumber(int count, int a[], int n, int num, int k) { // Base case when K // elements are chosen if(count == k) { set.add(num); return; } // Choose every element // and call the function for(int i = 0; i < n; i++) generateNumber(count + 1, a, n, num + a[i], k); } // Function to print // the distinct integers 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(); // prints all the // elements in the set while(set.isEmpty() == false) { while(i.hasNext()) { System.out.print(i.next() + " "); //set.remove(i.next()); } } } // Driver Code public static void main (String[] args) { int arr[] = {3, 8, 17, 5}; int n = arr.length; int k = 2; // Calling Function printDistinctIntegers(k, arr, n); } } |
Python3
# Python3 program to print all distinct # integers that can be formed by K numbers # from a given array of N numbers. # stores all the distinct integers formed s = set() # Function to generate all possible numbers def generateNumber(count, a, n, num, k): # Base case when K elements are chosen if k == count: # insert it in set s.add(num) return # Choose every element and call the function for i in range(0, n): generateNumber(count + 1, a, n, num + a[i], k) # Function to print the distinct integers def printDistinctIntegers(k, a, n): generateNumber(0, a, n, 0, k) print("The", len(s), "distinct integers are:") # prints all the elements in the set for i in sorted(s): print(i, end = " ") # Driver Code if __name__ == "__main__": a = [3, 8, 17, 5] n, k = len(a), 2 # Calling Function printDistinctIntegers(k, a, n) # This code is contributed by Rituraj Jain |
C#
// C# program to print all // distinct integers that can // be formed by K numbers from // a given array of N numbers. using System; using System.Collections.Generic; class GFG { // stores all the distinct // integers formed static SortedSet<int> set = new SortedSet<int>(); // Function to generate // all possible numbers public static void generateNumber(int count, int []a, int n, int num, int k) { // Base case when K // elements are chosen if(count == k) { set.Add(num); return; } // Choose every element // and call the function for(int i = 0; i < n; i++) generateNumber(count + 1, a, n, num + a[i], k); } // Function to print // the distinct integers 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(); // prints all the // elements in the set foreach(int sets in set) { Console.Write(sets + " "); } } // Driver Code public static void Main (String[] args) { int []arr = {3, 8, 17, 5}; int n = arr.Length; int k = 2; // Calling Function printDistinctIntegers(k, arr, n); } } // This code has been contributed by 29AjayKumar |
The 10 distinct integers are: 6 8 10 11 13 16 20 22 25 34
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all integers that are sum of powers of two given numbers
- Print N distinct numbers following the given operations
- Print combinations of distinct numbers which add up to give sum N
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array in O(n)
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Sum of all numbers that can be formed with permutations of n digits
- Rearrange numbers in an array such that no two adjacent numbers are same
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Count numbers formed by given two digit with sum having given digits
- Minimize the sum of squares of sum of N/2 paired formed by N numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Smallest subarray with k distinct numbers
- Count subsets having distinct even numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

