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”


  • Suraj Potti

    A solution for this question in Python 3
    size = int(input(“Enter the size of the array:”))
    arr = list(map(int,input().split())) #This will allow to enter all the elements of the array in a single line.
    read = 0
    i = 0
    while read < size:
    sample = max(arr[read:size])
    if(sample == arr[i]):
    print(arr[i])
    read += 1
    i += 1
    else:
    read += 1
    i += 1


  • HEMANTH GARLAPATI

    a=[16,19,4,3,8,3]
    b=len(a)
    count=2
    for i in range(0,b):
    for j in range(i+1,b):
    if a[i]>a[j]:
    count=0
    else:
    count=1
    break
    if count==0 :
    print(a[i],”is leader”)


  • mradul

    very easy solution

    #include
    void main()
    {
    puts(“enter the size of the array”);
    int size;
    scanf(“%d”,&size);
    int arr[size];
    for(int i=0;i=0;i–)
    {
    if(arr[i]>temp)
    temp=arr[i];
    else
    arr[i]=-1;
    }
    for(int i=0;i<size;i++)
    {
    if(arr[i]!=-1)
    printf("%d ",arr[i]);
    }

    }


  • Lvs Kumar

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Leader l=new Leader();
    Scanner scr=new Scanner(System.in);
    int n=scr.nextInt();
    int[] arr=new int[n];
    for(int i=0;i<n;i++){
    arr[i]=scr.nextInt();
    }
    int count=0;
    aa:
    for(int i=0;ii;j–){

    if(arr[i]>arr[j]){
    count++;

    }else{
    break bb;
    }}
    if(count==n-1-i){
    System.out.println(arr[i]);
    count=0;
    }else{

    count=0;
    }
    }
    }
    }


  • surya 1251

    Int n,a[100],max;
    Printf(“enter the size of array”);
    Scanf(“%d”,&n);
    max=n+1;
    Printf(“enter the array”);
    Scanf(“%d”,&a[]);
    For (int I=0;I[I+1]){
    Printf(“%d”,a[I]);
    }else
    {
    If (I>=max){
    Printf(a[I]);
    }else{
    return 0;
    }}}}


  • surya 1251

    int main()
    {
    Int n,a[100],max;
    Printf(“enter the size of array”);
    Scanf(“%d”,&n);
    max=n+1;
    Printf(“enter the array”);
    Scanf(“%d”,&a[]);
    For (int I=0;Ia[I+1])
    Printf(“%d”,a[I]);
    else
    {
    If (I>=max)
    Printf(a[I]);
    else
    return 0;
    }


  • Sudhanshu Nautiyal

    IN PYTHON
    x=[int(y) for y in input().split()]
    for i in range(len(x)+1):
    x.append(-1)
    z=x[(i+1):]
    s=max(z)
    if x[i]>s:
    print(x[i])


  • palvadi narendra

    public class Main
    {
    public static void main(String[] args) {
    int arr[]={16, 19, 4, 3, 8, 3};
    int k=0;
    for(int i=0;i<arr.length;i++)
    {
    int sum=0;
    k=arr[i];
    for(int j=i+1;jsum)
    System.out.print(k+””);
    }
    }
    }


  • Divya Vutukuri

    python code
    a=[16,19,4,3,8,3]
    leader=[a[len(a)-1]]
    for i in range(0,len(a)-1):
    count=0

    for j in a[i+1:]:
    if(a[i]>j):
    count=count+1
    if(count==len(a[i+1:])):
    leader.append(a[i])
    print(leader)


    • HEMANTH GARLAPATI

      can we do like this
      a=[16,19,4,3,8,3]
      b=len(a)
      count=2
      for i in range(0,b):
      for j in range(i+1,b):
      if a[i]>a[j]:
      count=0
      else:
      count=1
      break
      if count==0 :
      print(a[i],”is leader”)


    • HEMANTH GARLAPATI

      a=[16,19,4,3,8,3]
      b=len(a)
      count=2
      for i in range(0,b):
      for j in range(i+1,b):
      if a[i]>a[j]:
      count=0
      else:
      count=1
      break
      if count==0 :
      print(a[i],”is leader”)


  • Sanchi

    i think there is a better solution to this question

    import java.util.*;
    public class LeaderInArray {

    public static void main(String[] args) {
    // Scanner scn = new Scanner(System.in);
    // int n = scn.nextInt();
    // int[] a = new int[n];
    // for(int i = 0; i < n; i++){
    // a[i] = scn.nextInt();
    // }
    int[] a = {16, 19, 4, 3, 8, 3};
    leader(a);
    }
    public static void leader(int[] arr){
    Stack st = new Stack();
    st.push(arr[0]);
    for(int i = 1; i arr[i]){
    st.push(arr[i]);
    }
    else{
    while(st.size() > 0 && st.peek() < arr[i]){
    st.pop();
    }
    st.push(arr[i]);
    }
    }
    printStack(st);
    }
    public static void printStack(Stack s){
    if(s.size() == 0){
    return;
    }
    int x = s.pop();
    printStack(s);
    System.out.print(x + ” “);
    }

    }