0

I have an array that looks like this:

    [0] => Array
    (
        [1] => 5
        [2] => 4
        [3] => 3
        [5] => 1
        [7] => 1
        [8] => 2
        [9] => 3
        [10] => 4
        [11] => 5
    )

[1] => Array
    (
        [1] => 6
        [2] => 5
        [4] => 3
        [5] => 2
        [6] => 1
        [8] => 3
        [9] => 4
        [10] => 5
        [11] => 6
    )

[2] => Array
    (
        [1] => 7
        [2] => 6
        [3] => 5
        [4] => 4
        [5] => 3
        [6] => 2
        [7] => 3
        [8] => 4
        [11] => 7
    )

I have an order of operations I'm trying to go through and I really don't know where to go from here. Any suggestions would be of great help.

  1. First I give my class the number of items I want to return. For example here we'll use 4.

  2. I want to loop through and find the item in the array that has the lowest value.

  3. I want to look at the keys to the items around (it included) and be sure they're not missing a number if they are..reject it..

In this example the first one you would come to would be:

    [5] => 1

Now looking around it you see that the keys are missing some numbers. So no combination of 4 will get that to match any 4 in the proper order.

    [1] => 5
    [2] => 4
    [3] => 3
    [5] => 1  //this one
    [7] => 1
    [8] => 2
    [9] => 3

In this situation I want it to move onto the next case.

    [7] => 1

Notice this one will work due to the keys being 7,8,9,10.

    [7] => 1
    [8] => 2
    [9] => 3
    [10] => 4

This is what I would like returned first..but I don't even know how to begin to get there.

Further more there are situations like this..Say for example there are no 1's at all in the dataset..and only this lone 2 in the last one.

    [0] => Array
    (
        [1] => 5
        [2] => 4
        [3] => 3
        [5] => 3
        [7] => 3
        [8] => 3
        [9] => 3
        [10] => 4
        [11] => 5
    )

[1] => Array
    (
        [1] => 6
        [2] => 5
        [4] => 3
        [5] => 3
        [6] => 3
        [8] => 3
        [9] => 4
        [10] => 5
        [11] => 6
    )

[2] => Array
    (
        [1] => 7
        [2] => 6
        [5] => 3
        [6] => 2 // this one
        [7] => 3
        [8] => 4
        [11] => 7
    )

The following wont work:

    [6] => 2 // this one
    [7] => 3
    [8] => 4
    [11] => 7

but this one will:

        [5] => 3
        [6] => 2 // this one
        [7] => 3
        [8] => 4

I have no idea on how to approach this.. If someone could offer some advice it would be GREATLY appreciated. Thanks so much in advance.

2
  • Unclear what you mean by "missing a number" - in your example, [6] is missing too, but [7] is accepted. Presumably if it's about to accept, say, [7], then the following x must exist? Commented Nov 16, 2016 at 16:39
  • Yes! If it's going to accept 7. Then I would want 4,5,6 to exist.. or 5,6,8..or 8,9,10...given that my initial input is only looking for 4. Commented Nov 16, 2016 at 16:43

2 Answers 2

1

The following assumes your data is in an array called $data. I'll describe it in steps, then pull it all together as a function.

Step 1 find the min value:

$minValue=min($data);

Step 2 loop through the array looking for all values that are that value:

foreach($data as $index => $value){

    if($value == $minValue){
         // $index is a candidate!
    }
}

Step 3 Check if $valuesToReturn entries exist after index:

    $success=true;

    for($i=1;$i<=$valuesToReturn;$i++){

        if(!array_key_exists($index + $i,$data)){

            // Candidate failed.
            $success=false;
            break;

        }

    }

Step 4 If the candidate was successful, return it.

    if($success){
        return $index;
    }

Putting that all together, we get this:

function findSuitableIndex($data,$valuesToReturn){

    // Min:
    $minValue=min($data);

    foreach($data as $index => $value){

        if($value == $minValue){
            // $index is a candidate!

            // test if index is actually suitable:
            $success=true;

            for($i=1;$i<=$valuesToReturn;$i++){

               if(!array_key_exists($index + $i,$data)){

                    // Candidate failed.
                    $success=false;
                    break;

                }

            }

            if($success){
                return $index;
            }

        }

    }

    // If we fell down here, we failed to find any successful results.
    return -1;
}

Working sample:

Code on eval.in

Sign up to request clarification or add additional context in comments.

7 Comments

WOW! You are my hero!!!! Now I just need to make it return the working 4 results in an array. I suggest everyone thumbs up this right now because it's perfect. Thank you so much!
@JoelHasSimpleQuestions no problem! I just quickly re-read the question so one thing that might catch you out: if $valuesToReturn is 4, then it'll check the 4 entries after the 'candidate' index. Change $i<=$valuesToReturn to $i<$valuesToReturn if you only want it to check the following 3 :) To return an array, the easiest thing to do is copy/paste that same for loop and do e.g. $result[]=$data[$index+$i];
@JoelHasSimpleQuestions My personal choice would be to do that last part in a separate function too - that way you can get just the index if it's useful.
Ok Awesome!!!!!! My hero. Big time. Any suggestions on finding 5,6,8? That's kind of the part where I'm really confused now.
@JoelHasSimpleQuestions how do you mean? I.e. what's it got to search for? :)
|
0

Here are some suggestions. Your exact implementation would depend on your particular situation.

To loop through each element in the array, you could use a foreach loop.

foreach ($arr[0] as $index => $value) {
    // Here, $arr[0][$index] == $value;

}

To check if a key exists or not, you could use array_key_exists.

if ( !array_key_exists($index - 1, $arr[0]) ) {
    // The previous index is missing in the array.

}

A simple (but inefficient) way to find a contiguous sequence of k indices with the smallest value at the first index would be to find the smallest element and check if the contiguous sequence exists; if not, find the next smallest element and recursively check until you have completed processing the largest element.

You could also try finding all contiguous sequences of at least length k and then selecting the sequence with the smallest value at the first index.

Hope this helps!

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.