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.


6 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!

Credit Repair Dallas · February 11, 2022 at 10:04 am

Hello to all, as I am genuinely eager of reading this webpage’s post to be
updated regularly. It includes good information.

Leave a Reply

Avatar placeholder

Your email address will not be published.