Skip to content

Instantly share code, notes, and snippets.

@harmlessprince
Last active April 27, 2021 03:53
Show Gist options
  • Save harmlessprince/25f6bf8b23557c9582a02500043545c6 to your computer and use it in GitHub Desktop.
Save harmlessprince/25f6bf8b23557c9582a02500043545c6 to your computer and use it in GitHub Desktop.
This function returns start and end position of of given value in an array
<?php
function FindStartEnd($array, $val)
{
$type = gettype($array);
try {
if (!$array) {
throw new Exception("Expects parameter 1 to be array and should contain at least one value");
}
if (!is_numeric($val)) {
throw new Exception("Second value passed must be a number");
}
if(sort($array)){
$indexes = array_keys($array, $val);
if (!$indexes) {
return [-1, -1];
}
return [min($indexes), max($indexes)];
}
throw new Exception("expects parameter 1 to be array, $type given");
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
}
print_r(FindStartEnd([0,8,-2,5,0], 0));
@meekg33k
Copy link

Hey @harmlessprince, this is a neat solution and I like that your solution handles a good number of edge cases.

However, I think your solution currently throws an error when the array is empty. Ideally, it shouldn't throw an error. It should return [-1,-1] because part of the requirement is that if the element can't be found in the array, it should return [-1, -1].

What do you think?

@harmlessprince
Copy link
Author

harmlessprince commented Apr 23, 2021

Well, I decided to throw those errors due to the if statement stated in the question.
It was said "Your function should return [-1, - 1] IF val is not found in the array".

If there is now valid array, i felt it is fair to throw the errors.
That was why I decided to throw those errors 😁.

@meekg33k
Copy link

So based off the statement in the question, if you have an empty array it means that regardless of what val is, your function should always return [-1, -1] right because val will never be found in the empty array, right?

@harmlessprince
Copy link
Author

Yes very true, I tested that as well.
Then I realised you can't sort an empty array and which is another condition stated in the question.

So I figured wrapping it in the if statement is my safest bet, if the sorting returns a falsy, then the array can't be sorted otherwise, it enters the if block and execute

@meekg33k
Copy link

Right, so your code doesn't have to sort the empty array. What you can do instead could be to have a check like this:

if (count($array)) == 0) { return [-1, -1]

@meekg33k
Copy link

Also, do you think you can implement this in a more optimal way without sorting it?

@harmlessprince
Copy link
Author

I don't actually feel the sorting is necessary I just did because the question said so.

Sorting it added an nlogn time complexity on the solution.

If we remove the sorting, it time complexity should be n and space complexity is also constant

That is the little i know, maybe you should clarify what you mean by optimal

@harmlessprince
Copy link
Author

Here is another solution considering our previous discussions

function FindStartEnd($array, $val)
{
    if (!$array) {
        return [-1, -1];
    }
    if (!is_numeric($val)) {
        return [-1, -1];
    }
    if (count($array) == 0) {
        return [-1, -1];
    }
   
    $indexes = array_keys($array, $val);
        if (!$indexes) {
            return [-1, -1];
        }
        return [$indexes[0], $indexes[count($indexes)-1]];
}

print_r(FindStartEnd([0,8,-2,5,0], 0));

@meekg33k
Copy link

meekg33k commented Apr 25, 2021

This is a more optimal solution with improved time complexity because like you rightly mentioned, adding the sorting made the time complexity O(N log N).

At this point, the performance of your solution is now dependent on the implementation of the array_keys function. Do you know that works?

@harmlessprince
Copy link
Author

harmlessprince commented Apr 25, 2021

It is like a syntax sugar for a for loop that iterates to the given array and returns all the keys.

But when you give it a value, then only the keys with the specified value are returned.

That is what I know

@meekg33k
Copy link

Really commendable effort @harmlessprince, I like this solution a lot better even though it is still dependent on the performance of array_keys and count functions.

I have written a blog post here sharing my solution to this problem - a solution that avoids having to sort the array. Please read through and let me know what you think.

Thank you for participating once again and see you on Friday for Week 4 of #AlgorithmFridays.

@harmlessprince
Copy link
Author

Thank you very much and look forward to coming Friday.

By the way, the link is broken, I am getting a 404 error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment