fbpx

My solution to a “Daily Coding Problem” that I received in my mail today.

Given an array of numbers and an index i, return the index of the nearest larger number of the number at index i, where distance is measured in array indices.

For example, given [4, 1, 3, 5, 6] and index 0, you should return 3.

If two distances to larger numbers are the equal, then return any one of them. If the array at i doesn’t have a nearest larger integer, then return null.

Here’s my solution in,

oneFortyFour(arr: number[], i: number): number | null {
    if(arr == null || arr == undefined) {
        return null;
    }
    if(i >= arr.length) {
        return null;
    }
    let distance: number | null = null;
    let noAtIdxI = arr[i];
    for(let iter=0;iter < arr.length; iter++) {
        if(iter == i) continue;
        let valAtIter = arr[iter];
        if(valAtIter >= noAtIdxI) {
            if(distance  == null) {
                distance = Math.abs(iter - i);
            }
            else if(Math.abs(iter - i) < distance) {
                distance = iter - i;
            }
        }
    }
    return distance;
}

Like the blog? Subscribe for updates

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store. 

https://mydaytodo.com/apps/

Also, if you can leave a review on the App Store or Google Play Store, that would help too.


8 Comments

데일리라이브 · September 25, 2020 at 7:21 am

What a stuff of un-ambiguity and preserveness of valuable
know-how concerning unpredicted feelings.

  • 토토픽 · September 26, 2020 at 5:05 am

    Your mode of describing everything in this article is in fact pleasant, every one can easily understand it, Thanks a lot.

  • Homelighting · October 10, 2020 at 8:36 pm

    Good response in return of this matter with genuine arguments and telling everything concerning that.

  • Steve · October 15, 2020 at 8:20 pm

    Heya just wanted to give you a brief heads up and
    let you know a few of the pictures aren’t loading
    correctly. I’m not sure why but I think its a linking issue.

    I’ve tried it in two different browsers and both show the same results.

  • vertigo replica · October 15, 2020 at 8:21 pm

    Spot on with this write-up, I honestly believe that this
    site needs a great deal more attention. I’ll probably be returning to see more, thanks for the information!

  • Rosaline · October 15, 2020 at 10:19 pm

    Nice blog! Is your theme custom made or did you download it from
    somewhere? A design like yours with a few simple tweeks would
    really make my blog jump out. Please let me know where you got
    your design. Bless you

  • Emily · October 15, 2020 at 10:55 pm

    Howdy! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Many thanks

  • petite friture replica · October 15, 2020 at 11:39 pm

    Good blog post. I absolutely appreciate this website.

    Continue the good work!

    Leave a Reply

    Your email address will not be published. Required fields are marked *