Differences Between Combination Sum1, Combination Sum2 and Combination Sum 3

 Differences Between Combination Sum1, Combination Sum2 and Combination Sum 3

Leetcode has 4 types of Cobination Sum Problems. We should know the similarities and differences among all those problems:-

https://leetcode.com/problems/combination-sum/

https://leetcode.com/problems/combination-sum-ii/description/

https://leetcode.com/problems/combination-sum-iii/



class Solution {
    void func(List<List<Integer>> ans, List<Integer> cur, int[] nums, int remain, int st) {
        if (remain < 0) {
            return; // Base case: if remaining target is negative
        }
        if (remain == 0) {
            ans.add(new ArrayList<>(cur)); // Add valid combination to the result
            return;
        }
        for (int i = st; i < nums.length; i++) { // Try all candidates starting from index st
            cur.add(nums[i]); // Include nums[i]
            func(ans, cur, nums, remain - nums[i], i); // Allow reusing nums[i], so pass `i`
            cur.remove(cur.size() - 1); // Backtrack
        }
    }





    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        func(ans, new ArrayList<>(), candidates, target, 0);
        return ans;
    }
}
class Solution {
    void func(List<List<Integer>> ans, List<Integer> cur, int[] nums, int remain, int st) {
        if (remain < 0) {
            return; // Base case: if remaining target is negative
        }
        if (remain == 0) {
            ans.add(new ArrayList<>(cur)); // Add valid combination to the result
            return;
        }
        for (int i = st; i < nums.length; i++) {
            if (i > st && nums[i] == nums[i - 1]) {
                continue; // Skip duplicates
            }
            cur.add(nums[i]); // Include nums[i]
            func(ans, cur, nums, remain - nums[i], i + 1); // Move to next index
            cur.remove(cur.size() - 1); // Backtrack
        }
    }

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates); // Sort to handle duplicates
        List<List<Integer>> ans = new ArrayList<>();
        func(ans, new ArrayList<>(), candidates, target, 0);
        return ans;
    }
}

1. Combination Sum 1

This problem allows unlimited repetitions of the same number.

Combination Sum 2

This problem does not allow repetitions, and candidates can only be used once.
Also, duplicates in the input must be handled.

Combination Sum 3

This problem requires selecting exactly k numbers from 1-9 that sum up to the target, 

with no repetition allowed.

class Solution {
    void func(List<List<Integer>> ans, List<Integer> cur, int k, int n, int st) {
        if (k == 0 && n == 0) {
            ans.add(new ArrayList<>(cur)); // Add valid combination
            return;
        }
        if (k < 0 || n < 0) {
            return; // Base case: invalid conditions
        }
        for (int i = st; i <= 9; i++) {
            cur.add(i); // Include i
            func(ans, cur, k - 1, n - i, i + 1); // Move to next index
            cur.remove(cur.size() - 1); // Backtrack
        }
    }


    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> ans = new ArrayList<>();
        func(ans, new ArrayList<>(), k, n, 1); // Start from 1
        return ans;
    }
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.

Divide Intervals Into Minimum Number of Groups