-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask 2 (6).cpp
More file actions
46 lines (45 loc) · 726 Bytes
/
task 2 (6).cpp
File metadata and controls
46 lines (45 loc) · 726 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
#include <iostream>
using namespace std;
int maximun(int *arr,int n, int k){
int sum = 0;
int count = 0;
int last = -1;
for(int i = 0; i < n; i++){
for(int j = i+1; j<n; j++){
if(arr[i] < arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0; i < n; i++){
if(arr[i]!=last){
count++;
last = arr[i];
}
if(count<=k){
sum+=arr[i];
}
else{
break;
}
}
return sum;
}
int main()
{
int n;
cin>>n;
int arr[n];
cout<<"Enter the elements of the array";
for(int i = 0; i < n; i++){
cin>>arr[i];
}
int k;
cout<<"Enter the k";
cin>>k;
int res = maximun(arr,n,k);
cout<<"The maximun number is : "<<res<<endl;
return 0;
}