-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSumOfElements_between_k1Smallest_and_k2SmallestElements.cpp
52 lines (42 loc) · 1.45 KB
/
SumOfElements_between_k1Smallest_and_k2SmallestElements.cpp
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
// Program to find the sum of all elements of the array between k1 smallest and k2 smallest element of the Array.
#include <bits/stdc++.h>
using namespace std;
// This function will return the K_th smallest element in the array
int kth_Smallest(int arr[], int k, int size)
{
priority_queue<int> maxh; // max heap initialisation
for(int i=0; i<size; i++)
{
maxh.push(arr[i]); // pushing the array elements into the max heap
if(maxh.size()>k) // if the size of the max heap exceeds k , it will remove the top element of the max heap
{
maxh.pop();
}
}
return maxh.top(); // top element of the max heap will be the k_th smallest element of the array
}
// Driver Code
int main()
{
int k1,k2;
cin>>k1>>k2;
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
int first = kth_Smallest(arr,k1,n); // k1 smallest element in the array
int second = kth_Smallest(arr,k2,n);// k2 smallest element in the array
int sum=0;
for(int i=0; i<n; i++)
{
if(arr[i]>first and arr[i]<second)
{
sum=sum+arr[i]; // sum will store the sum of all elements of the array between k1 and k2 smallest elemenets of the array
}
}
cout<<"The sum is "<<sum<<endl;
}
// Time Complexity: O(n logk) + O(n) [O(nlogk) to find the kth Smallest element and O(n) to find the sum of in between elements in the array]