Sum of elements between k1'th and k2'th smallest elements

PROBLEM :
Given an array of integers and two numbers k1 and k2. Find sum of all elements between given two k1’th and k2’th smallest elements of array. It may be assumed that (1 <= k1 < k2 <= n) and all elements of array are distinct.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N, denoting the length of the array. Next line contains N space seperated integers of the array. Third line contains two space seperated integers denoting k1'th and k2'th smallest elements.

Output:
For each test case in a new line output the sum of all the elements between k1'th and k2'th smallest elements.

Constraints:
1<= T <= 100
1<= k1< K2 <= N <=50

Example:
Input
2
7
20 8 22 4 12 10 14
3 6
6
10 2 50 12 48 13
2 6

Output
26
73

Explanation:
Test Case 1 -
3rd smallest element is 10
6th smallest element is 20
Sum of all element between k1 & k2 is 12 + 14 = 26

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<algorithm>
#define max 100
int Sum_of_elements(int [],int ,int ,int ) ;
int main()
 {
int t,no,i,k1,k2 ;
int arr[max] ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
   cin>>k1>>k2 ;
   cout<<Sum_of_elements(arr,no,k1,k2)<<endl ;
}
return 0;
}

int Sum_of_elements(int arr[],int no,int k1,int k2)
{
    int i,sum ;
    sort(arr,arr+no) ;
    sum=0 ;
   
    for(i=k1;i<k2-1;i++)
       sum=sum+arr[i] ;
    return sum ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )