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); } }
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
@mradul temp variable to be a assigned a value first and condition is checked.
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”)
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]);
}
}
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;
}
}
}
}
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;
}}}}
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;
}
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])
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+””);
}
}
}
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)
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”)
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”)
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 + ” “);
}
}
amazing approach
Cocubes Coding Questions