
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Recursively print all sentences that can be formed from list of word lists in C++
- Print all increasing sequences of length k from first n natural numbers in C++
- All possible strings of any length that can be formed from a given string?
- Sum of all subsets of a set formed by first n natural numbers
- Print N lines of numbers such that every pair among numbers has a GCD K
- 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++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Print All Distinct Elements of a given integer array in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Print all n digit patterns formed by mobile Keypad in C++
- Count unique numbers that can be generated from N by adding one and removing trailing zeros in C++
Advertisements