# CoCubes Programming Question – 1

## Printing all the Leaders in an Array

Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side.

And the rightmost element is always a leader. For example int the array {16, 19, 4, 3, 8, 3}, leaders are 19, 8 and 3?

```#include<iostream>
using namespace std;

{
for (int i = 0; i < size; i++)
{
int j;
for (j = i+1; j < size; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j == size)
cout << arr[i] << " ";
}
}

int main()
{
int len, i;
cout<< "Enter the length of the array\n";
cin >> len;
int arr[len];
cout<< "Enter the elements of the array\n";
for( i=0; i<len; i++)
{
cin>>arr[i];
}

int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
```
```class LeadersInArray
{

/*Java Function to print leaders in an array */

void printLeaders (int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i + 1; j < size; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j == size)
System.out.print(arr[i] + " ");
}
}
/* Driver program to test above functions */
public static void main (String[]args)
{
int arr[] = new int[]{ 16, 17, 4, 3, 5, 2 };
int n = arr.length;
}

}
```

### 31 comments on “CoCubes Programming Question – 1”

• Vikas

int[] arr=[16, 19, 4, 3, 8, 3];
int n=arr.length-1;
for(int i=n-1;i>=0;i–)
{
{
}
}

• Ravi Chandra

n=int(input(“Enter the size of the array”))
array=list(map(int,input().split()))
for i in range(n-1):
c=0
for j in range(i+1,n):
if(array[i]>array[j]):
c=c+1
if(c==(n-(i+1))):
print(array[i],end=” “)
print(array[-1])