Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | C ++ program to find sum of all element between to K1 ' th ▁ and ▁ k2' th smallest elements in array ; Returns sum between two kth smallest elements of the array ; Sort the given array ; Below code is equivalent to ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumBetweenTwoKth ( int arr [ ] , int n , int k1 , int k2 ) { sort ( arr , arr + n ) ; int result = 0 ; for ( int i = k1 ; i < k2 - 1 ; i ++ ) result += arr [ i ] ; return result ; } int main ( ) { int arr [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int k1 = 3 , k2 = 6 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumBetweenTwoKth ( arr , n , k1 , k2 ) ; return 0 ; }