Leaders in an array
Problem :
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. The rightmost element is always a leader.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= A[i]<=100
Example:
Input:
2
6
16 17 4 3 5 2
5
1 2 3 4 0
Output:
17 5 2
4 0
-----------------------------------------------------------------------------
Methord 1 :
Use two loops. The outer loop runs from 0 to size – 1 and one by one picks all elements from left to right. The inner loop compares the picked element to all the elements to its right side. If the picked element is greater than all the elements to its right side, then the picked element is the leader.
-------------------------------------------------------------------------
//C++ code
#include<iostream>
using namespace std;
int main()
{
int a[100],test,no,i,j ;
cin>>test ;
while(test--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(a[j]==a[i])
break ;
if(a[j]>a[i])
break ;
}
if(j==no)
cout<<a[i]<<" " ;
}
cout<<endl ;
}
return 0;
}
-----------------------------------------------------------------------------
Methord 2 :
Scan all the elements from right to left in array and keep track of maximum till now. When maximum changes it’s value, print it.
------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,max ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
max=-1 ;
for(i=no-1;i>=0;i--)
{
if(a[i]>max)
{
cout<<a[i]<<" " ;
max=a[i] ;
}
}
cout<<endl ;
}
return 0;
}
-------------------------------------------------------------------------------
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. The rightmost element is always a leader.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= A[i]<=100
Example:
Input:
2
6
16 17 4 3 5 2
5
1 2 3 4 0
Output:
17 5 2
4 0
-----------------------------------------------------------------------------
Methord 1 :
Use two loops. The outer loop runs from 0 to size – 1 and one by one picks all elements from left to right. The inner loop compares the picked element to all the elements to its right side. If the picked element is greater than all the elements to its right side, then the picked element is the leader.
-------------------------------------------------------------------------
//C++ code
#include<iostream>
using namespace std;
int main()
{
int a[100],test,no,i,j ;
cin>>test ;
while(test--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(a[j]==a[i])
break ;
if(a[j]>a[i])
break ;
}
if(j==no)
cout<<a[i]<<" " ;
}
cout<<endl ;
}
return 0;
}
-----------------------------------------------------------------------------
Methord 2 :
Scan all the elements from right to left in array and keep track of maximum till now. When maximum changes it’s value, print it.
------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,max ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
max=-1 ;
for(i=no-1;i>=0;i--)
{
if(a[i]>max)
{
cout<<a[i]<<" " ;
max=a[i] ;
}
}
cout<<endl ;
}
return 0;
}
-------------------------------------------------------------------------------
Usmutmege-zo-1994 Marisol Soler https://marketplace.visualstudio.com/items?itemName=8credenabga.Descargar-Idle-Armada-gratuita
ReplyDeletebeltygeve
NenleoZfragdo Diana Brown Click
ReplyDeletethere
cobbblacdanre