hxtree/sumfinder

This program allows for an integer array to be passed in and will then output all the pairs that add up to the sum.

v0.1.0 2020-02-25 07:21 UTC

This package is auto-updated.

Last update: 2024-10-26 12:31:39 UTC


README

SumFinder accepts an array of integers and finds which operands sum equals the desired value.

CI Codacy Badge

Usage

require __DIR__ . '/../vendor/autoload.php';

$sum_finder = new SumFinder();
$sum_finder->setSumValue(10);
$sum_finder->setIntArray(1,1,2,4,4,5,5,5,6,7,9);

/*
 * output all pairs (includes duplicates and the reversed order pairs)
 * [1,9], [1,9], [4,6], [4,6], [5,5], [5,5], [5,5], [5,5], [5,5], [5,5]
 */
echo $sum_finder->getAllPairs() . PHP_EOL;

/*
 * output unique pairs only once (removes the duplicates but includes the reversed ordered pairs)
 * [1,9], [4,6], [5,5], [6,4], [9,1]
 */
echo $sum_finder->getUniquePairs() . PHP_EOL;

/*
 * output the same combo pair only once (removes the reversed ordered pairs)
 * [1,9], [4,6], [5,5]
 */
echo $sum_finder->getComboPairs() . PHP_EOL;

Installation

Via Composer

SumFinder is available on Packagist.

Install with Composer:

composer require hxtree/sumfinder

Examples

Learn how SumFinder can be used through our Docs.