K’th smallest element
PROBLEM :
Given an array and a number k where k is smaller than size of array, the task is to find the k’th smallest element in the given array. It is given that all array elements are distinct.
Input:
First Line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting size of the array. Second line contains N space separated integer denoting elements of the array. Third line of the test case contains an integer K.
Output:
Corresponding to each test case, print the desired output in a new line.
Constraints:
1<=T<=100
1<=N<=1000
K<N
Example:
INPUT
2
6
7 10 4 3 20 15
3
5
7 10 4 20 15
4
Output:
7
15
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
// can use mearge sort,quick sort for better time compexivity
#include<iostream>
using namespace std;
int Kth_smallest(int [],int ,int ) ;
void bubble(int [],int ) ;
int main()
{
int t,no,arr[1000],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
no=Kth_smallest(arr,no,k) ;
cout<<no<<endl ;
}
return 0;
}
int Kth_smallest(int arr[],int no,int k)
{
int i ;
bubble(arr,no) ;
return arr[k-1] ;
}
void bubble(int arr[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Given an array and a number k where k is smaller than size of array, the task is to find the k’th smallest element in the given array. It is given that all array elements are distinct.
Input:
First Line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting size of the array. Second line contains N space separated integer denoting elements of the array. Third line of the test case contains an integer K.
Output:
Corresponding to each test case, print the desired output in a new line.
Constraints:
1<=T<=100
1<=N<=1000
K<N
Example:
INPUT
2
6
7 10 4 3 20 15
3
5
7 10 4 20 15
4
Output:
7
15
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
// can use mearge sort,quick sort for better time compexivity
#include<iostream>
using namespace std;
int Kth_smallest(int [],int ,int ) ;
void bubble(int [],int ) ;
int main()
{
int t,no,arr[1000],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
no=Kth_smallest(arr,no,k) ;
cout<<no<<endl ;
}
return 0;
}
int Kth_smallest(int arr[],int no,int k)
{
int i ;
bubble(arr,no) ;
return arr[k-1] ;
}
void bubble(int arr[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment