Coders Packet

Print all Combinations of a Sum in given Array Using C++


This Project Prints all the Combination Elements of the given sum that elements of the Array.Input will be given by User i.e.., an Array and Sum K using C++ programming language.

Given an integer array of distinct elements By User. 

and sum K,size of array N. 

Have to print all the Combinations of the elements of the array that sums up to K. 

Note : The combinations may contain duplicates .

Example 1:

Input: a = [2,3,6,7],N=4,K=7 
Output: [[2,2,3],[7]] 

Example 2:

Input: a= [2,3,5],N=1 
Output: [[2,2,2,2],[2,3,3],[3,5]] 

Time Complexity : As we solved using recursion the Time complexity will be O(2^N).


  • 1 <= array size <= 30
  • 2 <= array[i] <= 40
  • All elements of candidates are distinct.
  • 1 <= K <= 40  

Download Complete Code


No comments yet