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”


  • Shambhavi Singh

    #include
    int main()
    {
    int i,a[100],n,max;
    printf(“enter number of elements in an array\n”);
    scanf(“%d”,&n);
    printf(“enter an array\n”);
    for(i=0;i=0;i–)
    {
    if(max<=a[i])
    {
    printf("%d",a[i]);
    max=a[i];
    }
    }
    return(0);
    }


    • Shambhavi Singh

      #include
      int main()
      {
      int i,a[100],n,max;
      printf(“enter number of elements in an array\n”);
      scanf(“%d”,&n);
      printf(“enter an array\n”);
      for(i=0;i=0;i–)
      {
      if(max<=a[i])
      {
      printf("%d",a[i]);
      max=a[i];
      }
      }
      return(0);
      }


  • Abhishek

    int leader(int a[],int n)
    {
    int m=a[n-1];
    for(int i=n-2;i>=0;i–)
    {
    if(a[i]>m)
    {
    cout<<a[i]<<" ";
    m=a[i];
    }
    }
    }


  • SaiManoj

    #include
    int main()
    {
    int n,a[100],i,co=0;
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    for(i=1;i<=n;i++)
    {
    co=0;
    for(int j=i+1;j=a[j])
    co++;
    }
    if(co==n-i)
    printf(“%d”,a[i]);
    }
    return 0;
    }


  • Ali

    #include
    int main(){
    int i=0,a[100],n;
    printf(“Enter the number of array elements”);
    scanf(“%d”,&n);
    printf(“\nEnter the array elements\n”);
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }
    displayLeader(n,a);
    }
    void displayLeader(int n,int a[]){
    int i=0,j=0,count=0,leader[100],temp,countleader=0;
    for(i=0;i<n;i++){
    for(j=i+1;j<n;j++){
    if(a[i]a[j]){
    printf(“\n%d\t%d”,a[i],a[j]);
    count++;
    }

    if(count==n-i-1){
    printf(“\nCOUNT: %d”,count);
    temp=a[i];
    leader[i]=temp;
    countleader++;
    }
    }
    if(i==n-1){ //For last element
    printf(“\ni= %d”,i);
    temp=a[n-1];
    leader[i]=temp;
    countleader++;
    }
    count=0;
    }
    printf(“\nThis is last element\n%d”,a[n-1]);
    printf(“\nLeader elements are : “);
    for(i=0;i0 && leader[i]<100000)
    printf("\n%d",leader[i]);
    }

    }


  • shivam mishra

    l=list(map(int,input().strip().split(” “)))
    l1=[]
    for i in range(len(l)):
    if i!=len(l)-1:
    for j in range(i+1,len(l)):
    flag=False
    if l[i]<l[j]:
    break
    else:
    flag=True
    if flag:
    l1.append(l[i])
    else:
    l1.append(l[i])
    for k in l1:
    print(k,end=" ")


  • Javed Ansari

    public class LeaderElement
    {
    static void Leader(int[] arr)
    {
    int size = arr.length;
    for(int i =0;i<size-1;i++)
    {
    int flag = 1;
    for(int j = i+1;j<size;j++)
    {
    if(arr[i]<arr[j])
    {
    flag = 0;
    break;
    }

    }
    if(flag == 1)
    {
    System.out.println(arr[i]);
    }
    }
    System.out.println(arr[size-1]);
    }

    public static void main(String[] args)
    {
    int[] arr = new int[] { 16,17,3,5,2 };
    Leader(arr);

    }
    }


  • MAKTUM

    a=list(map(int,input().split()))
    n=len(a)-1
    b=[]
    k=0
    for i in range(0,n):
    for j in range(i+1,n):
    if a[i]>a[j]:
    k=1
    else:
    k=0
    break
    if k==1:
    b.append(a[i])
    b.append(a[-1])
    print(b)


  • Rakesh Kumar

    #include
    int main()
    {
    int n;
    scanf(“%d”,&n);
    int i,a[n],j;
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
    int count=i+1;
    for(j=i+1;ja[j])
    {
    count++;
    }
    }
    if(count==j)
    {
    printf(“%d “,a[i]);
    }
    }
    }


  • Mohd Saif

    #include
    using namespace std;
    void leader_arr(int arr[],int n){
    vector temp_arr;
    temp_arr.push_back(arr[n-1]);
    int k=0;
    for(int i=n-2;i>=0;i–){
    if(arr[i]>temp_arr.back()){
    temp_arr.push_back(arr[i]);
    k+=1;
    }
    }
    for(int i=k;i>=0;i–){
    cout<<temp_arr[i]<>n;
    int arr[n];
    for(int i=0;i>arr[i];
    }
    leader_arr(arr,n);
    return 0;
    }


  • Omkar Warghade

    int main()
    {
    int size,i,temp=0;
    int a[]={16,19,4,3,8,3};
    size=sizeof(a)/sizeof(a[0]);

    for(i=0;i<size;i++)
    {

    for(int j=i+1;ja[j]){
    // cout<<"in the temp of i="<<i<<endl;
    temp=1;
    }else{
    temp=0;
    break;
    }
    }
    if(i==size-1){
    cout<<a[i]<<endl;
    break;
    }

    if(temp==1)
    cout<<a[i]<<endl;
    }

    return 0;
    }


  • prince

    a=list(map(int,input().split()))
    b=[]

    n=len(a)
    for i in range(n):
    m=0
    for j in range(i+1,n):
    if(a[i]>a[j]):
    m=m+1

    if(m==n-1-i):
    b.append(a[i])
    print(b)