Combination Sum
C++
// C++ program to find all combinations that
// sum to a given value
#include <bits/stdc++.h>
using namespace std;
// Print all members of ar[] that have given
void findNumbers(vector<int>& ar, int sum,
vector<vector<int> >& res,
vector<int>& r, int i)
{
// If current sum becomes negative
if (sum < 0)
return;
// if we get exact answer
if (sum == 0)
{
res.push_back(r);
return;
}
// Recur for all remaining elements that
// have value smaller than sum.
while (i < ar.size() && sum - ar[i] >= 0)
{
// Till every element in the array starting
// from i which can contribute to the sum
r.push_back(ar[i]); // add them to list
// recur for next numbers
findNumbers(ar, sum - ar[i], res, r, i);
i++;
// remove number from list (backtracking)
r.pop_back();
}
}
// Returns all combinations of ar[] that have given
// sum.
vector<vector<int> > combinationSum(vector<int>& ar,
int sum)
{
// sort input array
sort(ar.begin(), ar.end());
// remove duplicates
ar.erase(unique(ar.begin(), ar.end()), ar.end());
vector<int> r;
vector<vector<int> > res;
findNumbers(ar, sum, res, r, 0);
return res;
}
// Driver code
int main()
{
vector<int> ar;
ar.push_back(2);
ar.push_back(4);
ar.push_back(6);
ar.push_back(8);
int n = ar.size();
int sum = 8; // set value of sum
vector<vector<int> > res = combinationSum(ar, sum);
// If result is empty, then
if (res.size() == 0)
{
cout << "Emptyn";
return 0;
}
// Print all combinations stored in res.
for (int i = 0; i < res.size(); i++)
{
if (res[i].size() > 0)
{
cout << " ( ";
for (int j = 0; j < res[i].size(); j++)
cout << res[i][j] << " ";
cout << ")";
}
}
}
Also in C++:
- Title
- count function c++
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- primeros numeors primos menores que
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- what is iterator in c++?
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- getline not working c++
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- c++ lettura file
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- when ratings will be updated for codechef
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++