Q1) Kth smallest element
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
Example 1:
Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7.
Example 2:
Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15.
CODE : -
Question Link :- https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1#// { Driver Code Starts import java.io.*; import java.util.*; class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int k=sc.nextInt(); Solution ob = new Solution(); out.println(ob.kthSmallest(arr, 0, n-1, k)); } out.flush(); } } // } Driver Code Ends //User function Template for Java
class Solution{ public static int kthSmallest(int[] arr, int l, int r, int k) { Arrays.sort(arr); return arr[k-1]; } }
ReplyDeleteimport java.util.Arrays;
import java.util.Scanner;
public class KthElement {
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the no of element");
int ch=sc.nextInt();
System.out.println("enter k value");
int k=sc.nextInt();
int arr[]=new int [ch];
for(int i=0;i<ch;i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
System.out.println(arr[k-1]);
}
}