Reputation: 36937
Ok so lets say I have an array that can be 0 - X large x being in the tens of thousands, I know insane notion but none the less just for the sake of how vast the array can be. What I need to do is device a function that can take a couple parameters. One would be how many numbers am I looking to sum to make the number I want to check, the next would be the number I want to check for. The next would be the array itself.
At first I figured something like array_sum() would give me a little help but thats only for totaling the entire sum of the array. What I need to do is say I either want 1 - 10 different values again for sake of example to see if this will total what I am seeking. Now its possible I could think of something up on my own if I can only figure out how to check that 1-10 concept. Its most likely only going to be 2 at any given time but I want it dynamic for potential future needs. So anyone know of an algorithm concept I can come build up to check a given array like this? I know its possible, I just can't fully wrap my head around it at 3am in the morning.
EDIT
$test_case = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
function find_sum($totalby = 2, $expected = 0, $updown = "subtract")
{
//something here to take the total by and then check to see if expected is found
}
now what I mean to try and figure out is if any 2 numbers equal 0 when subtracted (although $updown could be "add" also for later expansion of this function. Anyway in this case scenario I should have specified if anything equals zero. Example I want to find if a 2 numbers equal zero together. Grant it Not exactly sure if my array above will find a zero result with just subtracting any 2 of the given numbers, but it expresses what type of array I am working with to holdfully achieve my goal.
Upvotes: 0
Views: 1259
Reputation: 6346
Sounds like all you need is to use array_slice before using array_sum.
Something like:
function subarr_sum($array,$offset,$length) {
$newarray = array_slice($array,$offset,$length);
return array_sum($newarray);
}
Upvotes: 1