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