Leaders in an arrray

So, the date has just changed to 20th August 2023, but has it changed for me? Nope. The day is still left for me, so why not a blog.
Actually I wanna learn Selenium, and that's the plan for the next 30 minutes after this blog. The last time I started doing that, I took a different turn towards Beautiful Soup, but that turn was really worth it and the efforts were quite fruitful considering the knowledge I got and the things were quite useful for many of my daily requirements.
I did various sorts of "Useful" scrapings and also tried for some automations projects that failed like a sinking boat but at least I tried, yeah you either win or you learn.
Well for those, automations I just think that maybe I don't have anyone to refer to, cause for many things that are not considered ethical no one's gonna teach you them on YT, believe me, no one. They would just say that they are but they won't, at the end they would just end the whole note on nothing. 

For the scraping parts also, there wasn't any help regarding anything, on YT, it's just those "unethical"(my favourite, if you know what I mean) people on stack overflow that help in such scenarios, I had to spend hours to find the bug that was in my code to finally come up with a solution that fixed the problem.

Well, this problem was of 17th aug maybe. Quite simple, just gotta go to the left from the right...if you will.
A thing to consider:-
;) The leader in an array is said to be the number which is >= to the greatest elem in the right side.
Now how to get these leaders?
Just maintain a variable to have the maximum elem in the right side, say maxRight, then the only thing you have to do is to compare this var with the elems while moving from right to left,
but but but, what if a new elem while traversing is greater than this elem, then you gotta update this maxRight, cause for the upcoming left elems, this current elem will become the maxRight,
If you understood this much, then you can go for the code part below, but if not, then don't, Do Not go for the code, part, why?
bcz I have seen in such easy level problems, people just see the code blindly 2-3 times and then think that they understood the problem but the problem is that they haven't.
The code is in JavaScript, cause I have started to handle the problems that I find easy in Js.
I am not gonna code these in Python or Cpp cause I am pretty sure about both of the languages now, and so I can get to a new one, and as Js is quite important and interesting and also this cannot be ignored so I gotta know it.

Code Implementation:-
leaders(arr, n){
    /* Fairly Self-Explanatory Algorithm */
   
    let ans=new Array(),rightGreatest=0;
    // console.log(ans);
    for(let i=arr.length-1;i>-1;i--){
        // console.log(arr[i]);
        if(arr[i]>=rightGreatest){
            // can either use, push with reverse at the end,
            // ans.push(arr[i]);
           
            // or use unshift to add the elems from the beginning of the array, as
            // required by the problem statement
            ans.unshift(arr[i]);
        }
        // update the rightGreatest, cause now, arr[i] is also contributing to it for the next
        // left element if > rightGreatest
        // if(arr[i]>rightGreatest){
        //     rightGreatest=arr[i];
        // }
       
        // a ternary version of the above if condition,
        rightGreatest=((arr[i]>rightGreatest)?arr[i]:rightGreatest);
    }
    // console.log(`This is the length of the new array: ${ans.push(-1)}`);
    // console.log(`This is the popped element of the array: ${ans.pop(-1)}`);
   
    // if would have used push so would have had to reverse the ans array at the end,
    // ans.reverse();
    return ans;
}

Comments

Popular Posts