-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThreeLargestElements
64 lines (57 loc) · 1.23 KB
/
ThreeLargestElements
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
61
62
63
#include<iostream>
using namespace std;
int main() {
int i,j,temp;
int arr[]={10, 4, 3, 50, 23, 90};
int n = sizeof(arr) / sizeof(arr[0]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Three Largest Elements in Array are: "<<endl;
for(i=0;i<3;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
//Method 2
#include<iostream>
using namespace std;
int main() {
int first, second, third;
int i,x;
int arr[]={10, 4, 3, 50, 23, 90};
int n = sizeof(arr) / sizeof(arr[0]);
first = second = third = 0;
for(int i = 0; i < n; i++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i] ;
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third)
{
third = arr[i];
}
}
cout << "Three largest elements are "
<< first << " " << second << " "
<< third << endl;
return 0;
}