FIND RUNNER UP SCORE-HACKERRANK
PROBLEM STATEMENT:
Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
This is a problem from hackerrank python programming practice module.
This program is to find second largest number from array
solution:
PYTHON:
find max element from array and remove all occurrences of max from array
find max from formed array
solution
APPROACH 1:
n=int(input())
a=list(map(int,input().split())
k=max(a)
a.remove(k)
for i in range(len(a)):
if(a[i]==k):
a[i]=-9999
print(max(a))
This prints second max number from array
APPROACH 2:
make list as a set then all the duplicates will be removed and find maximum from array
Remove the max from array
print(max from set)
n=int(input())
a=list(map(int,input().split())
k=max(a)
a=list(set(a))
a.remove(k)
print(max(a))
This prints second largest element from list
APPROACH 3:
n=int(input())
a=list(map(int,input().split())
k=max(a)
a=list(set(a))
a=sorted(a)
print(a[-2])
Here the list is converted to a set to remove duplicates
the list is sorted
print the last but one element as it is second largest element
NOTE:
Python provides a variety of built in methods for many operations.
C IMPLEMENTATION:
#include
int main()
{
int n,a[100],i,j,k;
scanf("%d",&n);
for( i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j]=a[j];
a[j+1]=temp;
}
}
}
k=a[n-1];
for(i=n-i;i>=0;i--)
{
if(a[i]!=k)
{
printf("%d ",a[i]);
break;
}
}
return 0;
}
Explanation:
here max element will be last position element.
This approach sorts the given array and iterates from last and if element not equal to max element then it will be second largest element.
HOPE THIS HELPS.....
DON'T STOP ! TAKE NEXT STEP AND START PRACTICING....