The Wayback Machine - https://web.archive.org/web/20240927195246/https://www.geeksforgeeks.org/combinational-sum/
Open In App

Combinational Sum

Last Updated : 13 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
28.28%
1.1L

Given an array of positive integers arr[] and an integer x, The task is to find all unique combinations in arr[] where the sum is equal to x. 
The same repeated number may be chosen from arr[] an unlimited number of times. Elements in a combination (a1, a2, …, ak) must be printed in non-descending order. (ie, a1 <= a2 <= … <= ak). If there is no combination possible print “Empty”.

Examples: 

Input: arr[] = 2, 4, 6, 8, x = 8
Output: 
[2, 2, 2, 2]
[2, 2, 4]
[2, 6]
[4, 4]
[8]

Approach:

Recursively find all combinations and if the current combination sums up to give X then add this combination in the final set of combinations.

Follow the below steps to implement the idea:

  • Sort the array arr[] and remove all the duplicates from the arr[] then create a temporary vector r. to store every combination and a vector of vector res.
  • Recursively follow: 
    • If at any time sub-problem sum == 0 then add that array to the res (vector of vectors).
    • Run a while loop till the sum – arr[I] is not negative and i is less than arr.size().
      • Push arr[i] in r and recursively call for i and sum-arr[i] ,then increment i by 1.
      • pop r[i] from back and backtrack. 

Below is the implementation of the above approach.

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 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

        // recursive call 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 << "Empty";
        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 << ")";
        }
    }
  return 0;
}
Java
// Java program to find all combinations that
// sum to a given value
import java.io.*;
import java.util.*;

class GFG {

    static ArrayList<ArrayList<Integer> >
    combinationSum(ArrayList<Integer> arr, int sum)
    {
        ArrayList<ArrayList<Integer> > ans
            = new ArrayList<>();
        ArrayList<Integer> temp = new ArrayList<>();

        // first do hashing since hashset does not always
        // sort
        //  removing the duplicates using HashSet and
        // Sorting the arrayList

        Set<Integer> set = new HashSet<>(arr);
        arr.clear();
        arr.addAll(set);
        Collections.sort(arr);

        findNumbers(ans, arr, sum, 0, temp);
        return ans;
    }

    static void
    findNumbers(ArrayList<ArrayList<Integer> > ans,
                ArrayList<Integer> arr, int sum, int index,
                ArrayList<Integer> temp)
    {

        if (sum == 0) {

            // Adding deep copy of list to ans

            ans.add(new ArrayList<>(temp));
            return;
        }

        for (int i = index; i < arr.size(); i++) {

            // checking that sum does not become negative

            if ((sum - arr.get(i)) >= 0) {

                // adding element which can contribute to
                // sum

                temp.add(arr.get(i));

                findNumbers(ans, arr, sum - arr.get(i), i,
                            temp);

                // removing element from list (backtracking)
                temp.remove(arr.get(i));
            }
        }
    }

    // Driver Code

    public static void main(String[] args)
    {
        ArrayList<Integer> arr = new ArrayList<>();

        arr.add(2);
        arr.add(4);
        arr.add(6);
        arr.add(8);

        int sum = 8;

        ArrayList<ArrayList<Integer> > ans
            = combinationSum(arr, sum);

        // If result is empty, then
        if (ans.size() == 0) {
            System.out.println("Empty");
            return;
        }

        // print all combinations stored in ans

        for (int i = 0; i < ans.size(); i++) {

            System.out.print("(");
            for (int j = 0; j < ans.get(i).size(); j++) {
                System.out.print(ans.get(i).get(j) + " ");
            }
            System.out.print(") ");
        }
    }
}
Python
# Python3 program to find all combinations that
# sum to a given value

def combinationSum(arr, sum):
    ans = []
    temp = []

    # first do hashing nothing but set{}
    # since set does not always sort
    # removing the duplicates using Set and
    # Sorting the List
    arr = sorted(list(set(arr)))
    findNumbers(ans, arr, temp, sum, 0)
    return ans

def findNumbers(ans, arr, temp, sum, index):
    
    if(sum == 0):
        
        # Adding deep copy of list to ans
        ans.append(list(temp))
        return
      
    # Iterate from index to len(arr) - 1
    for i in range(index, len(arr)):

        # checking that sum does not become negative
        if(sum - arr[i]) >= 0:

            # adding element which can contribute to
            # sum
            temp.append(arr[i])
            findNumbers(ans, arr, temp, sum-arr[i], i)

            # removing element from list (backtracking)
            temp.remove(arr[i])


# Driver Code
arr = [2, 4, 6, 8]
sum = 8
ans = combinationSum(arr, sum)

# If result is empty, then
if len(ans) <= 0:
    print("empty")
    
# print all combinations stored in ans
for i in range(len(ans)):

    print("(", end=' ')
    for j in range(len(ans[i])):
        print(str(ans[i][j])+" ", end=' ')
    print(")", end=' ')


# This Code Is Contributed by Rakesh(vijayarigela09)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Collections;

class GFG {

    static List<List<int> >
    combinationSum(List<int> arr, int sum)
    {
        List<List<int> > ans
            = new List<List<int> >();
        List<int> temp = new List<int>();

        // first do hashing since hashset does not always
        // sort
        //  removing the duplicates using HashSet and
        // Sorting the List

        HashSet<int> set = new HashSet<int>(arr);
        arr.Clear();
        arr.AddRange(set);
        arr.Sort();

        findNumbers(ans, arr, sum, 0, temp);
        return ans;
    }

    static void
    findNumbers(List<List<int> > ans,
                List<int> arr, int sum, int index,
                List<int> temp)
    {

        if (sum == 0) {

            // Adding deep copy of list to ans

            ans.Add(new List<int>(temp));
            return;
        }

        for (int i = index; i < arr.Count; i++) {

            // checking that sum does not become negative

            if ((sum - arr[i]) >= 0) {

                // Adding element which can contribute to
                // sum

                temp.Add(arr[i]);

                findNumbers(ans, arr, sum - arr[i], i,
                            temp);

                // removing element from list (backtracking)
                temp.Remove(arr[i]);
            }
        }
    }

    // Driver Code

    public static void Main()
    {
        List<int> arr = new List<int>();
        
        arr.Add(2);
        arr.Add(4);
        arr.Add(6);
        arr.Add(8);

        int sum = 8;

        List<List<int> > ans
            = combinationSum(arr, sum);

        // If result is empty, then
        if (ans.Count == 0) {
            Console.WriteLine("Empty");
            return;
        }

        // print all combinations stored in ans

        for (int i = 0; i < ans.Count; i++) {

            Console.Write("(");
            for (int j = 0; j < ans[i].Count; j++) {
                Console.Write(ans[i][j] + " ");
            }
            Console.Write(") ");
        }
    }
}

// This code is contributed by ShubhamSingh10
JavaScript
<script>
// Javascript program to find all combinations that
// sum to a given value

function combinationSum(arr, sum) {
    let ans = new Array();
    let temp = new Array();

    // first do hashing since hashset does not always
    // sort
    //  removing the duplicates using HashSet and
    // Sorting the arrayList

    let set = new Set([...arr]);
    arr = [...set];
    arr.sort()

    findNumbers(ans, arr, sum, 0, temp);
    return ans;
}

function findNumbers(ans, arr, sum, index, temp) {

    if (sum == 0) {

        // pushing deep copy of list to ans

        ans.push([...temp]);
        return;
    }

    for (let i = index; i < arr.length; i++) {

        // checking that sum does not become negative

        if ((sum - arr[i]) >= 0) {

            // pushing element which can contribute to
            // sum

            temp.push(arr[i]);

            findNumbers(ans, arr, sum - arr[i], i, temp);

            // removing element from list (backtracking)
            temp.splice(temp.indexOf(arr[i]), 1);
        }
    }
}

// Driver Code


let arr = []

arr.push(2);
arr.push(4);
arr.push(6);
arr.push(8);

let sum = 8;

let ans = combinationSum(arr, sum);

// If result is empty, then
if (ans.length == 0) {
    document.write("Empty");
}

// print all combinations stored in ans
for (let i = 0; i < ans.length; i++) {

    document.write("(");
    for (let j = 0; j < ans[i].length; j++) {
        document.write(" ", ans[i][j] + " ");
    }
    document.write(") ");
}

// This code is contributed by saurabh_jaiswal.
</script>

Output
 ( 2 2 2 2 ) ( 2 2 4 ) ( 2 6 ) ( 4 4 ) ( 8 )

Time Complexity: O(k*(2^n)) where n is the size of array, and k is average length
Auxiliary Space: O(k*x) where is x is number of possible combinations


 



Previous Article
Next Article

Similar Reads

Maximum sum subarray having sum less than given sum using Set
Given an array arr[] of length N and an integer K, the task is the find the maximum sum subarray with a sum less than K.Note: If K is less than the minimum element, then return INT_MIN. Examples: Input: arr[] = {-1, 2, 2}, K = 4 Output: 3 Explanation: The subarray with maximum sum which is less than 4 is {-1, 2, 2}. The subarray {2, 2} has maximum
7 min read
Maximum sum subarray having sum less than or equal to given sum
Given an array of non-negative integers and a sum. We have to find sum of the subarray having a maximum sum less than or equal to the given sum in the array. Note: Given array contains only non-negative integers. Examples: Input: arr[] = { 1, 2, 3, 4, 5 } sum = 11Output: 10Subarray having maximum sum is { 1, 2, 3, 4 }Input: arr[] = { 2, 4, 6, 8, 10
10 min read
Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
Given an integer N, find out the count of numbers(m) that satisfy the condition m + sum(m) + sum (sum(m)) = N, where sum(m) denotes the sum of digits in m. Given N <= 10e9. Examples: Input: 9 Output: 1 Explanation: Only 1 positive integer satisfies the condition that is 3, 3 + sum(3) + sum(sum(3)) = 3 + 3 + 3 = 9 Input: 9939 Output: 4 Explanatio
9 min read
Maximum subarray sum in O(n) using prefix sum
Given an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that Array. Examples: Input1 : arr = {-2, -3, 4, -1, -2, 1, 5, -3} Output1 : 7 Input2 : arr = {4, -8, 9, -4, 1, -8, -1, 6} Output2 : 9 Kadane's Algorithm solves this problem using Dynamic Programming approach in linear time. Here is another approach using Dynamic
8 min read
Construct sum-array with sum of elements in given range
You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {3, 6, 9, 12, 9} Explanation : sum_array[0] = arr[0
7 min read
Minimum array size after repeated replacement of even sum pair with sum
Given an array of size N. Choose a random pair of elements from the sequence such that their sum is even, delete those two elements from the sequence and insert their sum into the sequence instead in order to minimize the length of the array. Finally, print the minimum possible size of the array. Examples : Input : 88 98 1 7 3 Output : 2 By followi
6 min read
Count rows/columns with sum equals to diagonal sum
Given an n x n square matrix, count all rows and columns whose sum is equal to the sum of any principal diagonal or secondary diagonal. Examples: Input : n = 3 arr[][] = { {1, 2, 3}, {4, 5, 2}, {7, 9, 10}}; Output : 2 In first example sum of principal diagonal = (1 + 5 + 10) = 16 and sum of secondary diagonal = (3 + 5 + 7) = 15. Input: n = 4 arr[][
8 min read
Check if sum of digits in the left half is divisible by sum of digits in the right half in the largest permutation of N
Given a positive integer N, the task is to maximize the integer N by rearranging the digits and check if the sum of the left half digits is divisible by the sum of the right half digits or not. If found to be true, then print "Yes". Otherwise, print "No". If the number of digits(say D) in the given number N is odd, then consider any of the two poss
8 min read
Find Sum of all unique sub-array sum for a given array.
Given an array of n-positive elements. The sub-array sum is defined as the sum of all elements of a particular sub-array, the task is to find the sum of all unique sub-array sum. Note: Unique Sub-array sum means no other sub-array will have the same sum value. Examples: Input : arr[] = {3, 4, 5} Output : 40 Explanation: All possible unique sub-arra
10 min read
Min sum of set of positive integers so that sum of no two elements is K
Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7. Note: It is guaranteed that it is always possible to make a set tha
6 min read
Sum of sum of all subsets of a set formed by first N natural numbers
Given N, and ff(N) = f(1) + f(2) + ...... + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007. Examples: Input: 2 Output: 7 f(1) + f(2) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 Input: 3 Output: 31 f(1) + f(2) + f(3) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 f(3) = 1 + 2 +
11 min read
Count the number of pairs that have column sum greater than row sum
Given a matrix of size NxN. The task is to count the number of pair of indices (i, j) such that the sum of j-th column is greater than the sum of i-th row. Examples: Input : N = 2, mat[][] = {{1, 2}, {3, 4}} Output : 2 The 2 valid pairs are (1, 1) and (1, 2). Input : N = 3, mat[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; Output : 4 Approach: T
6 min read
Find sum of inverse of the divisors when sum of divisors and the number is given
Given an integer N and the sum of its divisors. The task is to find the sum of the inverse of the divisors of N.Examples: Input: N = 6, Sum = 12 Output: 2.00 Divisors of N are {1, 2, 3, 6} Sum of inverse of divisors is equal to (1/1 + 1/2 + 1/3 + 1/6) = 2.0Input: N = 9, Sum = 13 Output: 1.44 Naive Approach: Calculate all the divisors of the given i
3 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Find Sum of pair from two arrays with maximum sum
Given two arrays of positive and distinct integers. The task is to find a pair from the two arrays with maximum sum. Note: The pair should contain one element from both the arrays. Examples: Input : arr1[] = {1, 2, 3}, arr2[] = {4, 5, 6} Output : Max Sum = 9 Pair (3, 6) has the maximum sum. Input : arr1[] = {10, 2, 3}, arr2[] = {3, 4, 7} Output : M
6 min read
Largest number less than N with digit sum greater than the digit sum of N
Given an integer N, the task is to find the greatest number less than N such that the sum of its digits is greater than the sum of the digits of N. If the condition isn't satisfied for any number then print -1.Examples: Input: N = 100 Output: 99 99 is the largest number less than 100 sum of whose digits is greater than the sum of the digits of 100I
9 min read
Difference between Sum of Cubes and Sum of First N Natural Numbers
Given an integer N, find the absolute difference between sum of the cubes of first N natural numbers and the sum of first N natural numbers. Input: N = 3 Output: 30 Sum of first three numbers is 3 + 2 + 1 = 6 Sum of Cube of first three numbers is = 1 + 8 + 27 = 36 Absolute difference = 36 - 6 = 30 Input: N = 5 Output: 210 Approach: The sum of the c
4 min read
Minimize the sum of squares of sum of N/2 paired formed by N numbers
Given N numbers(N is an even number). Divide the N numbers into N/2 pairs in such a way that the sum of squares of the sum of numbers in pairs is minimal. The task is to print the minimal sum. Examples: Input: a[] = {8, 5, 2, 3} Output: 164 Divide them into two groups of {2, 8} and {3, 5} to give (2+8)2 + (3+5)2 = 164 Input: a[] = {1, 1, 1, 2, 2, 2
5 min read
Count numbers in given range such that sum of even digits is greater than sum of odd digits
Given two integers L and R denoting a range [L, R]. The task is to find the total count of numbers in the given range [L,R] whose sum of even digits is greater than the sum of odd digits. Examples: Input : L=2 R=10 Output : 4 Numbers having the property that sum of even digits is greater than sum of odd digits are: 2, 4, 6, 8 Input : L=2 R=17 Outpu
14 min read
Sequence with sum K and minimum sum of absolute differences between consecutive elements
Given two integers N and K, the task is to find a sequence of integers of length N such that the sum of all the elements of the sequence is K and the sum of absolute differences between all consecutive elements is minimum. Print this minimized sum. Examples: Input: N = 3, K = 56 Output: 1 The sequence is {19, 19, 18} and the sum of absolute differe
4 min read
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array
Given two arrays arr1[] and arr2[] of size N and M respectively, the task is to count the minimum number of swaps required between the two arrays in order to make the sum of the array arr1[] greater than the arr2[]. Examples: Input: arr1[] = {1, 3, 2, 4}, arr2[] = {6, 7, 8}Output: 1Explanation:Swapping arr1[0] with arr2[2] makes the sum of arr1[] e
10 min read
Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
Given the range L and R, count all numbers between L to R such that sum of digits of each number and sum of square of digits of each number is Prime.Note: 10 <= [L, R] <= 108 Examples: Input: L = 10, R = 20 Output: 4 Such types of numbers are: 11 12 14 16 Input: L = 100, R = 130 Output: 9Such types of numbers are : 101 102 104 106 110 111 113
9 min read
Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
Given an array of positive numbers, the task is to find the bitwise AND of the sum of non-prime numbers and the sum of prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 9 Sum of non-primes = 10 + 15 = 25 Sum of primes = 3 + 5 + 7 = 15 25 & 15 = 9Input: arr[] = {3, 4, 6, 7} Output: 10
9 min read
Maximum sum of two elements whose digit sum is equal
Content has been removed on Author's request.
1 min read
Divide array in two Subsets such that sum of square of sum of both subsets is maximum
Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pairs are as follows (1)2 + (2 + 3)2 = 26 (2)2 + (1
6 min read
Count the subarray with sum strictly greater than the sum of remaining elements
Given an array arr[] of N positive integers, the task is to count all the subarrays where the sum of subarray elements is strictly greater than the sum of remaining elements. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 6 Explanation: Subarrays are: {1, 2, 3, 4} - sum of subarray = 10, sum of remaining elements {5} = 5 {1, 2, 3, 4, 5} - sum of
12 min read
Find K numbers with sum equal to N and sum of their squares maximized
Given two integers N and K, the task is to find K numbers(A1, A2, …, AK) such that ?i=1KAi is equal to N and ?i=1KAi2 is maximum.Examples: Input: N = 3, K = 2 Output: 1 2 Explanation: The two numbers are 1 and 2 as their sum is equal to N and 12 + 22 is maximum.Input: N = 10, K = 3 Output: 1 8 1 Approach: The idea is to take number 1, K - 1 times a
3 min read
Construct an Array of size N in which sum of odd elements is equal to sum of even elements
Given an integer N which is always even, the task is to create an array of size N which contains N/2 even numbers and N/2 odd numbers. All the elements of array should be distinct and the sum of even numbers is equal to the sum of odd numbers. If no such array exists then print -1.Examples: Input: N = 8 Output: 2 4 6 8 1 3 5 11 Explanation: The arr
7 min read
Maximum sum of Array formed by replacing each element with sum of adjacent elements
Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent elements: 4 + 2 = 6 6 + 1 = 7 7 + 3 = 10 Array for
9 min read
Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
Given an array arr[] of N elements, the task is to derive a MultiSet having the numbers from the given array in possible repetitions, such that the sum of the MultiSet is strictly greater than given number P and if any of the element is removed, then the sum becomes strictly less than P. Print the number of times the corresponding element of given
10 min read