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?

 

58 comments on “CoCubes Programming Question – 1”


  • Sourabh

    #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;

    }


  • Shavy

    #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;
    }


  • Sakshi

    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])


  • sonali

    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);
    }
    }
    }


  • Raju

    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();
    }
    }


  • Pavan

    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])


  • Yogeshwar

    #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 << " ";
    }
    }


  • SHRAVANI

    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])


  • Vikas

    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
    }
    }


  • 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])