/*
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; // arr[0] is always considered a leader
    
        for (int i = 1; i < n; i++) {
            int max = arr[0]; // Assume the first element is the maximum
    
            for (int j = 0; j < i; j++) {
                if (arr[j] > max) {
                    max = arr[j];
                }
            }
    
            if (arr[i] > max) {
                count++;
            }
        }
    
        return count;
    }

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

Embed on website

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