# Function For An Output Of Indicies

## Recommended Posts

All,

I had someone request a function to be written. Here's what they said:

can you take a list of ints and get back a list of indices that add up to a variable 'X'?? here's what they asked for (as an example)...

Input: ([1,4,7,2], X=3) output: [[0,1]]

I'm not quite sure I'm following it. Anyone shed some light on this for me? I'm just not getting what they want, probably because I don't have formal training in coding. Vocabs are always mismatched.

thanks.

##### Share on other sites

I'm guessing that the example output is wrong. Your input has four 'indicies' of 1, 4, 7, and 2. The variable you are trying to add up to is 3. The sample output is 0 and 1? 0 + 1 = 1, 1 != 3. I would think the correct output is 1 & 2. Those are two if the indicies from the input that add up to the variable 3.

##### Share on other sites

Yes I realize. This is obvious math-based. What I was confused about was the terminology more than anything. Do you think there's anything else to read into this if the math would make sense? say something like this (which of course makes sense):

Input: ([1,4,7,2], X=3) output: [[0,1,2]].

the function will perform math to manipulate the input to the output. that's all. I thought they were using "integers" and "indicies" as interchangeable terms, that's all. they have a rep for doing these kinds of things.

think it's pretty simple? it sounds like. thanks.

##### Share on other sites

Indices is the multiple of index, so if they were mixing them with integers...

Anyway, let's say one assumes a standard enumerated array, that the numbers given in the example are the values, and that the numbers returned are the indices. If the task is to get the indices of the values that would sum up to the given number in X, then the return value should have been `0,3`.

##### Share on other sites

If the task is to get the indices of the values that would sum up to the given number in X, then the return value should have been `0,3`.

Christian,

Why does it make a difference as to whether the return is 0,3 or 0,1,2?? what's your thinking on it??

I will ask them why this is needed, but I know it's not urgent. this happens all the time.

##### Share on other sites

```\$arr = array (1,4,7,2);
echo '<pre>'.print_r(\$arr, 1).'</pre>';

/****** results *******
Array
(
 => 1
 => 4
 => 7
 => 2
)
***********************/
```

1, 2 are the integers that add up to 3

However, the question you posted was for the indices of those integer ie 0, 3

##### Share on other sites

Barand,

Let me just ask you this:

what would you personally write in PHP that takes 2 inputs (X, Y): 1) an array of integers (X) and 2) a summation target value (Y).

and then produces one output: an array / list of indicies that sum to input #2 (Y).

thanks. this is my last question here. this is already too long for being a little bit simple for the gurus...

##### Share on other sites

As I wouldn't know how many of the set of integers would be required to sum to to the target number, I'd write a recursive function to sum the various combinations.

##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.