-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3rd.cpp
More file actions
60 lines (59 loc) · 1.64 KB
/
3rd.cpp
File metadata and controls
60 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//array operations
//largest element smallest element
//second largest and second smallest
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter numeber of elements in an array :\n";
cin>>n;
int arr[n];
cout<<"Enter elements in the array :"<<endl;
for(int i=0;i<n;i++)
{
cout<<": ";
cin>>arr[i];
}
//most naive approach can be sort and print
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
if(n>=4)
{
cout<<"Largest element : "<<arr[n-1]<<endl;
cout<<"Second largest element : "<<arr[n-2]<<endl;
cout<<"Smallest element : "<<arr[0]<<endl;
cout<<"Second smallest element : "<<arr[1]<<endl;
}
else if(n==1)//checking for all the corner cases
{
cout<<"Largest element : "<<arr[0]<<endl;
cout<<"Second largest element : "<<arr[0]<<endl;
cout<<"Smallest element : "<<arr[0]<<endl;
cout<<"Second smallest element : "<<arr[0]<<endl;
}
else if(n==2)
{
cout<<"Largest element : "<<arr[1]<<endl;
cout<<"Second largest element : "<<arr[0]<<endl;
cout<<"Smallest element : "<<arr[0]<<endl;
cout<<"Second smallest element : "<<arr[1]<<endl;
}
else if(n==3)
{
cout<<"Largest element : "<<arr[2]<<endl;
cout<<"Second largest element : "<<arr[1]<<endl;
cout<<"Smallest element : "<<arr[0]<<endl;
cout<<"Second smallest element : "<<arr[1]<<endl;
}
}