
A company wishes to modify the technique by which tasks in the processing queue are executed. There are N processes with unique IDs from 0 to N-1. Each of these tasks has its own execution time. The company wishes to implement a new algorithm for processing tasks. For this purpose they have identified a value K By the new algorithm, the processor will first process the task that has the Kth shortest execution time. Write an algorithm to find the Kth shortest execution time.
Input
The first line of the input consists of two space-separated integers – numTasks and valueK representing the number of tasks (N) and the value K, which is used as reference, respectively. The second line consists of N space-separated integers – exectuionTimep exectuionTime2, exectuion7imeN representing the execution times of the tasks.
Output
Print an integer representing the Kth shortest execution time.
Constraints
0 < value K < numTasks < 106
0 < execution time < 106
0 < I < numTasks
Example
Input:
7 5
9 -3 8 -6 -7 18 10
Output:
9
Explanation:
The sorted list of execution times is [ -7, -6, -3, 8, 9,10, 18].
Solution: In C
#include <stdio.h>
int main()
{
int n,k;
scanf("%d %d", &n,&k);
int a[n],i,j,t;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("%d",a[k-1]);
}
Also Checkout