Snippets Collections
class Solution{
    //Function to find the leaders in the array.
    static ArrayList<Integer> leaders(int arr[], int n){
        int maxEle = arr[n-1];
        ArrayList<Integer> res = new ArrayList<>();
        //We start traversing the array from last element.
        for(int i=n-1; i>=0; i--) {
            //Comparing the current element with the maximum element stored. 
            //If current element is greater than max, we add the element.
		    if(arr[i] >= maxEle){
		        //Updating the maximum element.
		        maxEle = arr[i];
		        //Storing the current element in arraylist for leaders.
		//Reversing the arraylist.
		//returning the arraylist.
        return res;

Tue Feb 08 2022 05:35:58 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #arrays #practice #leaders

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension