/*
Given an array ar[N] , you have to find a number of leaders in the arry . 
Leader : An elemet ar[i] is said to be a leader if it is greater than the maximum of all elements present on the left of it i.e. [0,i-1]

Note : ar[0] is already considered a leader

Constraints : 1 <= N <= 10^5
              1 <= ar[i] <= 10^9

             
*/


import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    
    public static int countLeaders(int[] arr) {
        int n = arr.length;
        int count = 1;             
        int max = arr[0];         
    
        // Iterate through the array from left to right
        for (int i = 1; i < n; i++) {
            if (arr[i] > max) {
                max = arr[i];
                count++;             // The current element is a leader
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] arr = {3, 2, 4, 5, 2, 7};
        System.out.println("Number of leaders in the array: " + countLeaders(arr));
    }
    
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: