Generating desired combinations in JavaScript


In the given problem statement we have to generate the desired combinations of the given inputs. And program the solution with the help of Javascript programming .

Understanding the Problem

In this problem, we will be given integers from 1 to 9. Each combination should be a unique collection of numbers. The function should identify all the possible combinations of size m that and these items with size m should have the sum of n.

For example if the value of m is 3 and n is 6, then the combination should be [1, 2, 3]. So the items in the output array 1, 2 and 3 are between 1 to 9 and the sum of all these items is 6 because the value of n is 6 and m's value is 3 so there are 3 items present.

Logic for the given Problem

The logic for the algorithm is to create a code for finding combinations of the numbers which have the sum as target value n and have length of m. To get these combinations we will employ a recursive function. Then we will define a function and use the m and n as input to the function. There is also a search function to find all the possible combinations by starting with the given value. And lastly the function will start the recursive process and return the results.

Algorithm

Step 1: Define the values of the size of combinations as m and the limit of the target sum as n.

Step 2: Create a function which will find the sum of all the possible combinations and pass the parameters as m and n.

Step 3: Save the generated combinations of array in a new array and initialize it as empty.

Step 4: Create a recursive function to search the various combinations of numbers starting with the particular value.

Step 5: This function will end when both the values m and n are equal to zero. In this case the current item is pushed into the result array to store the combination.

Step 6: If the base case is not present then the function continues to explore combinations. if the value exceeds 9 then that means there are no more numbers to be considered.

Step 7: Then we will call the search function recursively if the current value is included then concatenate it with the prefix.

Step 8: The code will call the search function with the initial parameters to start the recursive process. And give all the required combinations as a result.

Example

const m = 3, n = 9;
//Function for finding all possible combinations
const findSum = (m, n) => {
   const res = [];

   const search = (from, prefix, m, n) => {
      if (m === 0 && n === 0) {
         res.push(prefix);
         return;
      }
      if (from > 9) return;

      search(from + 1, prefix.concat(from), m - 1, n - from);
      search(from + 1, prefix, m, n);
   };

   search(1, [], m, n);
   return res;
};

console.log(findSum(m, n));

Output

[ [ 1, 2, 6 ], [ 1, 3, 5 ], [ 2, 3, 4 ] ]

Complexity

As the function explores all the possible combinations the time complexity is proportional to the number of valid combinations. And the combinations depend on both the values of m and n. So the time complexity is O(2^n), here n is the target sum. The space complexity of the code is calculated by the space used to store the result of combinations. So in the worst case when all the possible combinations are valid then the space complexity is also O(2^n).

Conclusion

The function we have created is effectively generating all the possible combinations of numbers which sum up to a given target with the specified length using backtracking technique. But the important thing is to consider the exponential growth in the time and space complexity which can affect the performance for the large input values.

Updated on: 14-Aug-2023

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements