In: Computer Science
C Programming Language
Problem Title : Museum Heist
Jojo loves art. In his free time, he usually goes to the museum and admires the artwork there. since Jojo loves are so much, he is planning a heist at the museum. Jojo knows the price of every piece of art and doesn't want to raise suspicion, so he decided to steal the second most expensive art piece. It is guaranteed that there are at least two art pieces with different prices.
Format Input
A line with integer N followed by another line of N numbers denoting the value of Ai.
Format Output
A single line of integer denoting the price of the second most expensive art piece.
Constraints
¶ 2 ≤ N ≤ 10^5
¶ 1 ≤ Ai ≤ 10^9
Sample Input 1
5
1 7 2 5 3
Sample Output 1
5
Sample Input 2
4
1 7 9 9
Sample Output 2
7
Sample Input 3
7
11 11 13 13 13 12 11
Sample Output 3
11
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
int main()
{
int i,n,a[100],l=0,f=0,s=0,temp;
scanf("%i",&n);//inputing N
for(i=0;i<n;i++)
{
scanf("%i",&a[i]);//inputting N number of items
}
for(i=0;i<n;i++)
{
f=0;
if(a[i]>l)//check if each element is less than l
{
f++;//flag to check if a new large element is found
temp=l;//to store the previous large
l=a[i];//new large is assigned
}
if(f==1)//if new large is found
{
s=temp;//second largest is the previous large element
}
if((a[i]>s)&&(a[i]!=l))/*condition useful if large element is first element in array*/
{
s=a[i];
}
}
printf("%d",s);
return 0;
}