SORT 9
QUESTION DESCRIPTION
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
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
TEST CASE 2
INPUT
INPUT
1
6
12 45 32 1 22 5
2 5
OUTPUT34
#include <stdio.h>
int main() {
int t,size,arr[101],i,j,k1,k2,sum,temp;
scanf("%d",&t);
while(t-->0)
{
sum=0;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
for(i=0;i<size-1;i++)
{
for(j=0;j<size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
scanf("%d", &k1);
scanf("%d", &k2);
for(i=k1; i<k2-1; i++)
sum+=arr[i];
printf("%d\n", sum);
}
return 0;
}
Comments
Post a Comment