forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest3Elements.cpp
More file actions
46 lines (33 loc) · 809 Bytes
/
largest3Elements.cpp
File metadata and controls
46 lines (33 loc) · 809 Bytes
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
/* find the largest 3 elements of the array */
#include<iostream>
#include<algorithm>
using namespace std;
int findLargest(int *arr,int size){
sort(arr, arr + size);
int check = 0, count = 1;
for(int i = 1; i <= n; i++)
{
if(count<4)
{
if(check != arr[n - i])
{
// to handle duplicate values
cout << arr[n - i] << " ";
check = arr[n - i];
count++;
}
}
else
break;
}
}
int main(){
int size;
cin>>size;
int *arr = new int[size];
for(int i = 0 ; i < size ; i++){
cin>>arr[i];
}
int result = findLargest(arr,size);
cout<<"The largest element is "<<result<<endl;
}