#include<bits/stdc++.h>
using namespace std;
int secondSmallest(int arr[],int n)
{
if(n<2)
return -1;
int small = INT_MAX;
int second_small = INT_MAX;
int i;
for(i = 0; i < n; i++)
{
if(arr[i] < small)
{
second_small = small;
small = arr[i];
}
else if(arr[i] < second_small && arr[i] != small)
{
second_small = arr[i];
}
}
return second_small;
}
int secondLargest(int arr[],int n)
{
if(n<2)
return -1;
int large=INT_MIN,second_large=INT_MIN;
int i;
for (i = 0; i < n; i++)
{
if (arr[i] > large)
{
second_large = large;
large = arr[i];
}
else if (arr[i] > second_large && arr[i] != large)
{
second_large = arr[i];
}
}
return second_large;
}
int main() {
int arr[]={1,2,4,7,7,5};
int n=sizeof(arr)/sizeof(arr[0]);
int sS=secondSmallest(arr,n);
int sL=secondLargest(arr,n);
cout<<"Second smallest is "<<sS<<endl;
cout<<"Second largest is "<<sL<<endl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IHNlY29uZFNtYWxsZXN0KGludCBhcnJbXSxpbnQgbikKewogICAgaWYobjwyKQogICAgICAgIHJldHVybiAtMTsKICAgIGludCBzbWFsbCA9IElOVF9NQVg7CiAgICBpbnQgc2Vjb25kX3NtYWxsID0gSU5UX01BWDsKICAgIGludCBpOwogICAgZm9yKGkgPSAwOyBpIDwgbjsgaSsrKSAKICAgIHsKICAgICAgIGlmKGFycltpXSA8IHNtYWxsKQogICAgICAgewogICAgICAgICAgc2Vjb25kX3NtYWxsID0gc21hbGw7CiAgICAgICAgICBzbWFsbCA9IGFycltpXTsKICAgICAgIH0KICAgICAgIGVsc2UgaWYoYXJyW2ldIDwgc2Vjb25kX3NtYWxsICYmIGFycltpXSAhPSBzbWFsbCkKICAgICAgIHsKICAgICAgICAgIHNlY29uZF9zbWFsbCA9IGFycltpXTsKICAgICAgIH0KICAgIH0KICAgcmV0dXJuIHNlY29uZF9zbWFsbDsgICAgIAp9CmludCBzZWNvbmRMYXJnZXN0KGludCBhcnJbXSxpbnQgbikKewoJaWYobjwyKQoJcmV0dXJuIC0xOwogICAgaW50IGxhcmdlPUlOVF9NSU4sc2Vjb25kX2xhcmdlPUlOVF9NSU47CiAgICBpbnQgaTsKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIAogICAgewogICAgICAgIGlmIChhcnJbaV0gPiBsYXJnZSkgCiAgICAgICAgewogICAgICAgICAgICBzZWNvbmRfbGFyZ2UgPSBsYXJnZTsKICAgICAgICAgICAgbGFyZ2UgPSBhcnJbaV07CiAgICAgICAgfQogCiAgICAgICAgZWxzZSBpZiAoYXJyW2ldID4gc2Vjb25kX2xhcmdlICYmIGFycltpXSAhPSBsYXJnZSkgCiAgICAgICAgewogICAgICAgICAgICBzZWNvbmRfbGFyZ2UgPSBhcnJbaV07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHNlY29uZF9sYXJnZTsgICAgICAgICAgICAgICAgCn0KCmludCBtYWluKCkgewogICAgaW50IGFycltdPXsxLDIsNCw3LDcsNX07ICAKICAgIGludCBuPXNpemVvZihhcnIpL3NpemVvZihhcnJbMF0pOwogICAgICAgIGludCBzUz1zZWNvbmRTbWFsbGVzdChhcnIsbik7CiAgICAgICAgaW50IHNMPXNlY29uZExhcmdlc3QoYXJyLG4pOwogICAgY291dDw8IlNlY29uZCBzbWFsbGVzdCBpcyAiPDxzUzw8ZW5kbDsKICAgIGNvdXQ8PCJTZWNvbmQgbGFyZ2VzdCBpcyAiPDxzTDw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==