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”


  • Abhinav

    python working solution:

    a,p=[],[]
    for i in range(0,6):
    a.append(int(input()))
    q=len(a)-1
    for i in range(0,len(a)):
    count=0
    for j in range(i+1,len(a)):
    if a[i]<a[j]:
    count=0
    break
    else :
    count+=1
    if count!=0:
    p.append(a[i])
    p.append(a[q])
    print(p)


  • A.

    c=0
    n=int(input())
    a=[]
    for i in range(0,n):
    a.append(int(input()))
    for i in range(0,n):
    for j in range(i+1,n):
    if a[i]<=a[j]:
    break
    else:
    c=c+1
    if c==n-i-1:
    print(a[i])
    c=0


  • A.

    #include
    using namespace std;
    int main()
    {
    int n,c=0;
    cin>>n;
    int a[n];
    for(int i=0;i>a[i];
    for(int i=0;i<n;i++)
    {
    for(int j=i+1;j<n;j++)
    {
    if(a[i]<=a[j])
    break;
    else
    c++;
    }
    if(c==n-i-1)
    cout<<a[i]<<" ";
    c=0;
    }
    }


  • Adithya

    C solution
    int n=6;
    int a[]={16,19,4,3,8,3};
    int count;
    for(int i=0; i<n; i++)
    {
    count=0;
    for(int j=i+1; j a[j])
    {
    count++;
    }
    }
    if(count == n-i-1)
    {
    printf(“%d “,a[i]);
    }
    }


  • Suvojit

    #include

    using namespace std;

    int main()
    {
    int n,i,j,max,check;
    cin >> n;
    int arr[n];
    for(i=0; i> arr[i];
    }
    for(i=0; i<n-1; i++){
    max = arr[i];
    check = 0;
    for(j=i+1; jmax){
    check = 1;
    break;
    }
    }
    if(check == 0){
    cout << arr[i] << "\t";
    }
    }
    cout << arr[n-1];

    return 0;
    }


  • d6mr07

    list = [16, 19, 4, 3, 8, 3]
    leaders = []

    for i in range(len(list)-1):
    if list[i] >= max(list[i+1:]):
    leaders.append(list[i])
    leaders.append(list[-1])
    print(*leaders)


  • Prachi

    n=list(map(int,input().split()))

    def lead(n):
    for i in range(0,len(n)-1,1):
    if(n[i]<=n[i+1]):
    print(n[i+1], end=',')
    print(n[len(n)-1])

    lead(n)


  • Mahesh

    int n;
    int arr[n];
    cin >> n;
    for(int i=0;i> arr[i];
    int b[n],c[n],p=1;
    for(int i=0;i<n;i++)
    b[i]=arr[n-1-i];
    c[0]=b[0];
    for(int i=1;i=0;i–)
    cout << c[i] << " ";


  • Shiva

    a=[16,19,4,3,8,3];k=len(a)-1;c=0;
    for i in range(len(a)):
    for j in range(i+1,len(a)):
    if a[i]>a[j]:
    c+=1
    if c==k:
    print(a[i],end=” “)
    c=0;k-=1


  • Pushkar

    public class LeadersInArray {

    void printLeaders(int arr[],int n)
    {
    int max = 0 ;
    for(int i= n-1 ;i>=0 ;i–)
    if(arr[i] >= max)
    {
    max = arr[i] ;
    System.out.print(arr[i] +” “);
    }
    }

    public static void main(String[] args) {
    LeadersInArray lead = new LeadersInArray ();
    int arr[] = new int[]{ 16, 19, 4, 3, 5, 2 };
    int n = arr.length;
    lead.printLeaders (arr, n);

    }

    }


  • surya

    l=[16,19,4,3,8,3]
    n=len(l)
    for i in range(n-1):
    for j in range(i+1,n):
    if(l[i]<l[j]):
    break
    else:
    print(l[i],end=',')
    print(l[n-1])


  • IPE2K17

    PYTHON 3.8
    n = list(map(int, input().split()))
    l=[]for i in range(len(n)):
    for j in range(i+1, len(n)):
    if (n[i]<=n[j]):
    break
    if (j==len(n)-1):
    l.append(n[i])
    print(l)


    • Mahesh

      int n;
      int arr[n];
      cin >> n;
      for(int i=0;i> arr[i];
      int b[n],c[n],p=1;
      for(int i=0;i<n;i++)
      b[i]=arr[n-1-i];
      c[0]=b[0];
      for(int i=1;i=0;i–)
      cout << c[i] << " ";


    • Chethan

      def leaders(arr: list) -> int:
      maxElem = arr[-1]
      result = [arr[-1]]
      for i in range(len(arr) – 2,-1,-1):
      if arr[i] > maxElem:
      result.append(arr[i])
      maxElem = arr[i]
      return result

      print(leaders([16, 19, 4, 3, 8, 3]))