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?
C++
Java
C++
#include<iostream> using namespace std; void findLeaders(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) 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]); findLeaders(arr, n); return 0; }
Java
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) { LeadersInArray lead = new LeadersInArray (); int arr[] = new int[]{ 16, 17, 4, 3, 5, 2 }; int n = arr.length; lead.printLeaders (arr, n); } }
Login/Signup to comment
#include”bits/stdc++.h”
#define ll long long
using namespace std;
void cal()
{
int res[] = {16, 19, 4, 3, 8, 3};
int len = sizeof(res) / sizeof(res[0]);
for(int i=0;i<len;i++)
{
int ne=0;
for(int j=i+1;j<len;j++)
{
if(res[i]<=res[j])
{
ne = 1;
break;
}
}
if(ne==0)
{
cout<<res[i]<<"\n";
}
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cal();
return 0;
}
#include
using namespace std;
int main()
{
int arr[]={16, 19, 4, 3, 8, 3};
stacks;
s.push(arr[0]);
for(int i=1;is.top()){
s.pop();
}
else break;
}
s.push(arr[i]);
}
vectorv;
while(!s.empty()){
v.insert(v.begin(),s.top());
s.pop();
}
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
return 0;
}
#include
using namespace std;
int main()
{
int n,max=0,i;
cin>>n;
int a[n];
vectorb;
for(i=0;i>a[i];
max=a[n-1];
b.push_back(max);
for(i=n-1;i>=0;i–)
{
if(max=0;i–)
{
cout<<b[i]<<" ";
}
return 0;
}
// Time complexity O(n)
#include
using namespace std;
int main()
{
int n,max=0,i;
cin>>n;
int a[n];
vectorb;
for(i=0;i>a[i];
max=a[n-1];
b.push_back(max);
for(i=n-1;i>=0;i–)
{
if(max=0;i–)
{
cout<<b[i]<<" ";
}
return 0;
}
// Time complexity O(n)
here’s the code in python 🙂
l=[int(x) for x in input().split()]
for i in range(0,len(l)):
flag=0
for j in range(0,i):
if(l[i]<l[j]):
flag=1
break
if(flag==0):
print(l[i])
public class MyClass {
public static void main(String args[]) {
int n=6;
int []arr={16,19,4,5,1,3};
int j=0;
for(int i=0;i<n;i++)
{
int high=arr[i];
for(j=i+1;j<n;j++)
{
if(high<=arr[j])
break;
}
if(j==n)
System.out.println(high);
}
}
}
import java.util.*;
public class Leaders {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the leader points: “);
String str[] = sc.nextLine().split(” “);
int a[] = new int[str.length];
boolean firstisBig = true;
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(str[i]);
}
for (int j = 0; j < a.length; j++) {
int first = a[j];
for (int k = j+1; k a[k]){
firstisBig = true;
}else{
firstisBig = false;
break;
}
}
if(firstisBig) System.out.print(first+” “);
}
sc.close();
}
}
lst=list(map(int,input().split()))
n=len(lst)
temp=0
for i in range(n):
j=i+1
temp=0
while jlst[j]:
temp+=1
#print(lst[i],lst[j],temp,i)
j+=1
if temp==(n-i-1):
print(lst[i])
#include
int main(){
int array[6] = {16, 19, 4, 3, 8, 3};
int size = sizeof(array) / sizeof(int);
for(int i = 0; i < size; i++){
int c = array[i];
bool is_lead = true;
for(int j = i; j < size; j++){
if(c < array[j]){
is_lead = false;
break;
}
}
if(is_lead)
std::cout << c << " ";
}
}
list1=list(map(int,input().split()))
x=len(list1)
for i in range(0,x):
for j in range(i+1,x):
print(i)
print(j)
if list1[i]<list1[j]:
break
if j==x-1:
print(list1[i])
int[] arr=[16, 19, 4, 3, 8, 3];
int n=arr.length-1;
int curr_leader=arr[n];
print(curr_leader);
for(int i=n-1;i>=0;i–)
{
if(arr[i]>curr_leader)
{
curr_leader=arr[i];
print(curr_leader
}
}
Thanks Vikas, for contributing this code
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])