# How to Find Leaders in an Array

In this example, we’ll find leaders in an array.

An element of an array is a leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader.

```Input:
int[] arr1 = { 9, 178, 40, 3, 5, 2 };

Output:
[2, 5, 40, 178]
```

## 1. Java

### Solution with time complexity O(n2)

```public class LeadersInArray {

public static void main(String[] args) {
int[] arr = {9, 17, 40, 3, 50, 2};
}

for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[i]) {
break;
}
}
}
}
}
}
```

Output:

```[2, 50]
```

### Solution with time complexity O(n)

```public class LeadersInArray {

public static void main(String[] args) {
int[] arr = {19, 56, 23, 3, 55, 2};
}

int leaderFromRight = arr[arr.length - 1];

for (int i = arr.length - 2; i >= 0; i--) {
}
}
}
}
```

Output:

```[2, 55, 56]
```

Complete Java Solutions can be found here: Github Link

### More Programming Questions:

#### Strings:

The complete list can be found here: Practise Programming Questions