Given a list, the task is to find Kth’s Smallest and largest element in the given Unsorted list in Python.
Examples:
Example1:
Input:
Given List =[19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25] Given k=6
Output:
The given list before sorting is [19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25] The given list after sorting is [1, 4, 9, 10, 16, 19, 23, 24, 25, 25, 25, 36, 49, 49, 81, 144, 225, 600, 900] The 6 th smallest element in the given list is [ 19 ] The 6 th largest element in the given list is [ 49 ]
Example2:
Input:
Given list = [8, 45, 29, 19, 75, 68, 42, 13, 7, 9, 6, 4, 5, 96, 97, 91, 49, 72] Given K =3
Output:
The given list before sorting is [8, 45, 29, 19, 75, 68, 42, 13, 7, 9, 6, 4, 5, 96, 97, 91, 49, 72] The given list after sorting is [4, 5, 6, 7, 8, 9, 13, 19, 29, 42, 45, 49, 68, 72, 75, 91, 96, 97] The 3 th smallest element in the given list is [ 6 ] The 3 th largest element in the given list is [ 91 ]
Program to Find K’th Smallest/Largest Element in Unsorted Array or List in Python
Below are the ways to find Kth’s Smallest and largest element in the given Unsorted list in Python.
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Method #1: Using Sorting (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Give the value of K as static input and store it in a variable.
- Sort the given list using the sort() function which sorts the given list in ascending order.
- We can get the Kth smallest element in the given list by printing the k-1 index element in this sorted list.
- We can get the Kth largest element in the given list by printing the -k index element in this sorted list( ‘-‘ refers to negative indexing).
- The Exit of the program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvnlst = [19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25] print('The given list before sorting is', gvnlst) # Give the value of K as static input and store it in a variable. kth = 6 # Sort the given list using the sort() function # which sorts the given list in ascending order. gvnlst.sort() print('The given list after sorting is', gvnlst) # We can get the Kth smallest element in the given list # by printing the k-1 index element in this sorted list. print('The', kth, 'th smallest element in the given list is [', gvnlst[kth-1], ']') # We can get the Kth largest element in the given list # by printing the -k index element in this sorted list # ( '-' refers to negative indexing). print('The', kth, 'th largest element in the given list is [', gvnlst[-kth], ']')
Output:
The given list before sorting is [19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25] The given list after sorting is [1, 4, 9, 10, 16, 19, 23, 24, 25, 25, 25, 36, 49, 49, 81, 144, 225, 600, 900] The 6 th smallest element in the given list is [ 19 ] The 6 th largest element in the given list is [ 49 ]
Method #2: Using Sorting (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the value of K as user input and store it in a variable.
- Sort the given list using the sort() function which sorts the given list in ascending order.
- We can get the Kth smallest element in the given list by printing the k-1 index element in this sorted list.
- We can get the Kth largest element in the given list by printing the -k index element in this sorted list( ‘-‘ refers to negative indexing).
- The Exit of the program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvnlst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the value of K as user input and store it in a variable. kth = int(input('Enter some random Kth Number = ')) print('The given list before sorting is', gvnlst) # Sort the given list using the sort() function # which sorts the given list in ascending order. gvnlst.sort() print('The given list after sorting is', gvnlst) # We can get the Kth smallest element in the given list # by printing the k-1 index element in this sorted list. print('The', kth, 'th smallest element in the given list is [', gvnlst[kth-1], ']') # We can get the Kth largest element in the given list # by printing the -k index element in this sorted list # ( '-' refers to negative indexing). print('The', kth, 'th largest element in the given list is [', gvnlst[-kth], ']')
Output:
Enter some random List Elements separated by spaces = 8 45 29 19 75 68 42 13 7 9 6 4 5 96 97 91 49 72 Enter some random Kth Number = 3 The given list before sorting is [8, 45, 29, 19, 75, 68, 42, 13, 7, 9, 6, 4, 5, 96, 97, 91, 49, 72] The given list after sorting is [4, 5, 6, 7, 8, 9, 13, 19, 29, 42, 45, 49, 68, 72, 75, 91, 96, 97] The 3 th smallest element in the given list is [ 6 ] The 3 th largest element in the given list is [ 91 ]
Related Programs:
- python program to find the odd occurring element in an array list
- python program to find the minimum index of a repeating element in an array list
- python program to find a fixed point in a given array or list
- python program to find pair with the greatest product in an array or list
- python program to find a number repeating and missing in an array or list
- python program to find maximum product quadruple in an array or list
- python program to find the missing number in an array list