
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
In this problem, we are given an array of N integers and a number K. Our task is to print all distinct numbers that can be created by adding any K elements from the array. While choosing any number can be repeated K times.
Let’s take an example to understand the problem −
Input: array = {2, 5, 13, 9} K = 2 Output: 2,7,15,11,10,18,14,26,22 Explaination: 2 elements added : 2+2=4, 2+5=7, 2+13=15, 2+9=11, 5+5=10, 5+13=18, 5+9=14, 13+13=26, 13+9=22, 9+9=18
To solve this problem, we will find all combinations of the k element from the array. For this, we will use recursion which will be called recursively to generate numbers. To avoid duplicate values, we will store the numbers in a set.
Example
The code will show the implementation of our solution −
#include <bits/stdc++.h> using namespace std; set<int> distNumbers; void generateNumberFromArray(int count, int arr[], int n, int num, int k) { if (k == count) { distNumbers.insert(num); return; } for (int i = 0; i < n; i++) { generateNumberFromArray(count + 1, arr, n, num + arr[i], k); } } void printDistinctIntegers(int k, int arr[], int n) { generateNumberFromArray(0, arr, n, 0, k); cout<<"The "<<distNumbers.size()<<" distinct integers are:\n"; while (!distNumbers.empty()) { cout << *distNumbers.begin() <<"\t"; distNumbers.erase(*distNumbers.begin()); } } int main() { int arr[]={ 2, 5, 13, 9 }; int n=4; int k=2; printDistinctIntegers(k, arr, n); return 0; }
Output
The 9 distinct integers are − 4 7 10 11 14 15 18 22 26
- Related Articles
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Print all integers that are sum of powers of two given numbers in C++
- Print all increasing sequences of length k from first n natural numbers in C++
- Print N lines of numbers such that every pair among numbers has a GCD K
- All possible strings of any length that can be formed from a given string?
- Recursively print all sentences that can be formed from list of word lists in C++
- Python Program to print all distinct uncommon digits present in two given numbers
- Print All Distinct Elements of a given integer array in C++
- Sum of all subsets of a set formed by first n natural numbers
- Check a Subarray is Formed by Consecutive Integers from a Given Array in Java
- Print the nearest prime number formed by adding prime numbers to N
- Find if n can be written as product of k numbers in C++
- Python program to print all distinct elements of a given integer array.
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++

Advertisements