0

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.

2
  • 2
    could you at least post a function signature and maybe a little implementation to show us clearly what you want... Wall of text is not of any help I'm afraid :S Commented Mar 26, 2012 at 10:26
  • Yea seems as such.. Ill edit in a second to see if I can't further explain it to some point Commented Mar 26, 2012 at 10:35

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.