php - Find combination(s) sum of element(s) in array whose sum equal to a given number

Php - Find combination(s) sum of element(s) in array whose sum equal to a given number

You can find combinations of elements in an array whose sum equals a given number using recursion. Here's a PHP example:

<?php function findCombinations($array, $target, $start = 0, $path = [], &$result = []) { if ($target === 0) { $result[] = $path; return; } for ($i = $start; $i < count($array); $i++) { if ($array[$i] <= $target) { findCombinations($array, $target - $array[$i], $i, [...$path, $array[$i]], $result); } } } function printCombinations($result) { if (empty($result)) { echo "No combinations found.\n"; return; } foreach ($result as $combination) { echo "[" . implode(", ", $combination) . "]\n"; } } // Example usage $array = [2, 4, 6, 8]; $target = 8; $result = []; findCombinations($array, $target, 0, [], $result); printCombinations($result); ?> 

This script defines a function findCombinations that uses recursion to find combinations of elements in the array whose sum equals the target. The printCombinations function is used to print the result.

Replace the $array and $target variables with your own array and target sum. The script will output all combinations of elements whose sum equals the given target.

Note: This approach finds all possible combinations, including repeated elements. If you want to find unique combinations, additional logic may be needed.

Examples

  1. PHP find combination sum in array

    • Code:
      function findCombinationSum($arr, $target) { $result = []; $current = []; findCombination($arr, $target, 0, $current, $result); return $result; } function findCombination($arr, $target, $start, $current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($arr[$i] <= $target) { $current[] = $arr[$i]; findCombination($arr, $target - $arr[$i], $i, $current, $result); array_pop($current); } } } // Usage: $combinations = findCombinationSum([2, 3, 6, 7], 7); 
    • Description: Find combinations of elements in the array that sum up to a given target using recursion in PHP.
  2. PHP combination sum dynamic programming

    • Code:
      function findCombinationSumDP($arr, $target) { $dp = array_fill(0, $target + 1, []); $dp[0] = [[]]; foreach ($arr as $num) { for ($i = $num; $i <= $target; $i++) { foreach ($dp[$i - $num] as $combination) { $dp[$i][] = array_merge($combination, [$num]); } } } return $dp[$target]; } // Usage: $combinations = findCombinationSumDP([2, 3, 6, 7], 7); 
    • Description: Find combinations using dynamic programming to solve the combination sum problem in PHP.
  3. PHP find all combinations of array elements

    • Code:
      function findAllCombinations($arr) { $result = []; $total = count($arr); for ($i = 0; $i < (1 << $total); $i++) { $combination = []; for ($j = 0; $j < $total; $j++) { if ($i & (1 << $j)) { $combination[] = $arr[$j]; } } $result[] = $combination; } return $result; } // Usage: $combinations = findAllCombinations([2, 3, 6, 7]); 
    • Description: Generate all possible combinations of array elements in PHP using bitwise manipulation.
  4. PHP find combination sum using backtracking

    • Code:
      function findCombinationSumBacktrack($arr, $target) { $result = []; $current = []; backtrack($arr, $target, 0, $current, $result); return $result; } function backtrack($arr, $target, $start, &$current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($arr[$i] <= $target) { $current[] = $arr[$i]; backtrack($arr, $target - $arr[$i], $i, $current, $result); array_pop($current); } } } // Usage: $combinations = findCombinationSumBacktrack([2, 3, 6, 7], 7); 
    • Description: Use backtracking to find combinations of elements in the array that sum up to a given target in PHP.
  5. PHP find combination sum with no repetition

    • Code:
      function findCombinationSumNoRepetition($arr, $target) { $result = []; $current = []; sort($arr); // Ensure sorted to avoid duplicates backtrackNoRepetition($arr, $target, 0, $current, $result); return $result; } function backtrackNoRepetition($arr, $target, $start, &$current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($i > $start && $arr[$i] === $arr[$i - 1]) { continue; // Skip duplicates to avoid repetition } if ($arr[$i] <= $target) { $current[] = $arr[$i]; backtrackNoRepetition($arr, $target - $arr[$i], $i + 1, $current, $result); array_pop($current); } } } // Usage: $combinations = findCombinationSumNoRepetition([10, 1, 2, 7, 6, 1, 5], 8); 
    • Description: Find combinations with no repetition of elements in the array that sum up to a given target in PHP.
  6. PHP find combination sum with unique elements

    • Code:
      function findCombinationSumUnique($arr, $target) { $result = []; $current = []; sort($arr); // Ensure sorted for uniqueness backtrackUnique($arr, $target, 0, $current, $result); return $result; } function backtrackUnique($arr, $target, $start, &$current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($i > $start && $arr[$i] === $arr[$i - 1]) { continue; // Skip duplicates for unique combinations } if ($arr[$i] <= $target) { $current[] = $arr[$i]; backtrackUnique($arr, $target - $arr[$i], $i + 1, $current, $result); array_pop($current); } } } // Usage: $combinations = findCombinationSumUnique([10, 1, 2, 7, 6, 1, 5], 8); 
    • Description: Find combinations with unique elements in the array that sum up to a given target in PHP.
  7. PHP find combination sum with variable target

    • Code:
      function findVariableTargetCombinationSum($arr, $target) { $result = []; $current = []; sort($arr); // Ensure sorted for optimized search backtrackVariableTarget($arr, $target, 0, $current, $result); return $result; } function backtrackVariableTarget($arr, $target, $start, &$current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($arr[$i] <= $target) { $current[] = $arr[$i]; backtrackVariableTarget($arr, $target - $arr[$i], $i, $current, $result); array_pop($current); } } } // Usage: $combinations = findVariableTargetCombinationSum([2, 3, 5], 8); 
    • Description: Find combinations with a variable target in the array using backtracking in PHP.
  8. PHP find combination sum using memoization

    • Code:
      function findCombinationSumMemoization($arr, $target) { $memo = []; return backtrackMemoization($arr, $target, 0, $memo); } function backtrackMemoization($arr, $target, $start, &$memo) { if ($target === 0) { return [[]]; } if (isset($memo[$target])) { return $memo[$target]; } $result = []; for ($i = $start; $i < count($arr); $i++) { if ($arr[$i] <= $target) { $combinations = backtrackMemoization($arr, $target - $arr[$i], $i, $memo); foreach ($combinations as $combination) { $result[] = array_merge([$arr[$i]], $combination); } } } $memo[$target] = $result; return $result; } // Usage: $combinations = findCombinationSumMemoization([2, 3, 6, 7], 7); 
    • Description: Find combinations of elements in the array that sum up to a given target using memoization in PHP.
  9. PHP find combination sum with fixed-size combinations

    • Code:
      function findFixedSizeCombinationSum($arr, $target, $k) { $result = []; $current = []; backtrackFixedSize($arr, $target, $k, 0, $current, $result); return $result; } function backtrackFixedSize($arr, $target, $k, $start, &$current, &$result) { if ($target === 0 && count($current) === $k) { $result[] = $current; return; } for ($i = $start; $i < count($arr); $i++) { if ($arr[$i] <= $target) { $current[] = $arr[$i]; backtrackFixedSize($arr, $target - $arr[$i], $k, $i + 1, $current, $result); array_pop($current); } } } // Usage: $combinations = findFixedSizeCombinationSum([1, 2, 3, 4], 5, 2); 
    • Description: Find combinations with a fixed-size (k) that sum up to a given target in the array using backtracking in PHP.
  10. PHP find combination sum with no extra space

    • Code:
      function findCombinationSumNoExtraSpace($arr, $target) { $result = []; $current = []; sort($arr); // Ensure sorted for optimized search backtrackNoExtraSpace($arr, $target, 0, $current, $result); return $result; } function backtrackNoExtraSpace($arr, $target, $start, &$current, &$result) { if ($target === 0) { $result[] = $current; return; } for ($i = $start; $i < count($arr) && $arr[$i] <= $target; $i++) { $current[] = $arr[$i]; backtrackNoExtraSpace($arr, $target - $arr[$i], $i, $current, $result); array_pop($current); // Skip duplicates while ($i < count($arr) - 1 && $arr[$i] === $arr[$i + 1]) { $i++; } } } // Usage: $combinations = findCombinationSumNoExtraSpace([1, 2, 2, 2, 5], 5); 
    • Description: Find combinations with no extra space usage that sum up to a given target in the array using backtracking in PHP.

More Tags

assembly topshelf sim800 winrm cpu-registers labview rm regression asp.net-mvc-controller picasso

More Programming Questions

More Fitness Calculators

More Chemistry Calculators

More Investment Calculators

More Statistics Calculators