Last Updated : 02 Aug, 2023
Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can be solved.
Input : test_tup = (3, 7, 1, 18, 9), k = 2
Output : (3, 1, 9, 18)Input : test_tup = (3, 7, 1), k=1
Output : (1, 7)
Method #1 : Using sorted() + loop
The combination of above functionalities can be used to solve this problem. In this, we perform the sort operation using sorted(), and the problem of extraction of max and min K elements using loop.
# Python3 code to demonstrate working of
# Maximum and Minimum K elements in Tuple
# Using sorted() + loop
# initializing tuple
test_tup = (5, 20, 3, 7, 6, 8)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing K
K = 2
# Maximum and Minimum K elements in Tuple
# Using sorted() + loop
res = []
test_tup = list(sorted(test_tup))
for idx, val in enumerate(test_tup):
if idx < K or idx >= len(test_tup) - K:
res.append(val)
res = tuple(res)
# printing result
print("The extracted values : " + str(res))
The original tuple is : (5, 20, 3, 7, 6, 8) The extracted values : (3, 5, 8, 20)
Time complexity: O(n log n) where n is the length of the input tuple.
Auxiliary space: O(n) where n is the length of the input tuple
Method #2 : Using list slicing + sorted()
The combination of above functions can be used to solve this problem. In this, we perform the task of max, min extraction using slicing rather than brute force loop logic.
# Python3 code to demonstrate working of
# Maximum and Minimum K elements in Tuple
# Using slicing + sorted()
# initializing tuple
test_tup = (5, 20, 3, 7, 6, 8)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing K
K = 2
# Maximum and Minimum K elements in Tuple
# Using slicing + sorted()
test_tup = list(test_tup)
temp = sorted(test_tup)
res = tuple(temp[:K] + temp[-K:])
# printing result
print("The extracted values : " + str(res))
The original tuple is : (5, 20, 3, 7, 6, 8) The extracted values : (3, 5, 8, 20)
Time complexity: O(n log n), where n is the length of the tuple.
Auxiliary space: O(n), where n is the length of the tuple.
Method #3 : Using heapq module:
Python3
import heapq
test_tup = (5, 20, 3, 7, 6, 8)
# printing original tuple
print("The original tuple is : " + str(test_tup))
K = 2
smallest = heapq.nsmallest(K, test_tup)
largest = heapq.nlargest(K, test_tup)
result = tuple(sorted(smallest + largest))
print("The extracted values : " +str(result))
#This code is contributed by Jyothi pinjala
The original tuple is : (5, 20, 3, 7, 6, 8) The extracted values : (3, 5, 8, 20)
Time Complexity: O(n log k) n is the number of elements in tuple and k is the value of the constant representing the number of largest and smallest elements to be sorted
Auxiliary Space: O(k) k is the elements
Method 4 : using the built-in functions min() and max() and a loop to extract the K elements.
step-by-step approach :
# Python3 code to demonstrate working of
# Maximum and Minimum K elements in Tuple
# Using built-in functions and loop
# initializing tuple
test_tup = (5, 20, 3, 7, 6, 8)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing K
K = 2
# Find the minimum and maximum elements in the tuple
min_val = min(test_tup)
max_val = max(test_tup)
# Create two lists to store the K minimum and maximum elements
min_list = []
max_list = []
# Loop through the tuple and add elements to the appropriate list
for elem in test_tup:
if elem <= max_val:
if len(max_list) < K:
max_list.append(elem)
else:
max_list.remove(min(max_list))
max_list.append(elem)
max_val = max(max_list)
if elem >= min_val:
if len(min_list) < K:
min_list.append(elem)
else:
min_list.remove(max(min_list))
min_list.append(elem)
min_val = min(min_list)
# Combine the two lists and convert the result back to a tuple
result = tuple(min_list + max_list)
# Print the original tuple and the extracted values
print("The extracted values : " + str(result))
The original tuple is : (5, 20, 3, 7, 6, 8) The extracted values : (5, 8, 5, 3)
The time complexity of this method is O(nK), where n is the length of the tuple.
The auxiliary space is O(K) to store the minimum and maximum lists.
Method#5 : Using while loop + sort()Algorithm :
# Python program to print maximum and minimum k elements in the tuple
# Initializing the tuple
tup = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# Printing the original tuple
print('Given tuple is: ', tup)
min1 = []
max1 = []
# Converting tuple into list
tup = list(tup)
k = 2
i = 0
# Traversing for k min and max elements using while
while i < k:
min1.append(min(tup))
tup. remove(min(tup))
i = i+1
i = 0
while i < k:
max1. append(max(tup))
tup. remove(max(tup))
i = i+1
# Joining two lists
res = min1+max1
# sorting the list using sort()
res.sort()
# Converting the list into tuple
res = tuple(res)
# Printing the tuple with k min and max elements in the tuple
print('The minimum ', k, 'and maximum ', k, 'elements in the tuple are', res)
# Thos code is contributed by SHAIK HUSNA
Given tuple is: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) The minimum 2 and maximum 2 elements in the tuple are (0, 1, 8, 9)
Time Complexity : O(kn), where k is the number of minimum and maximum elements to be found, and n is the size of the original tuple
Auxiliary Space : O(k)
Method 6:Using a loop and two lists1.Define the input tuple and the number of maximum or minimum elements to extract
2.Define two empty lists to store the maximum and minimum K elements
3.Iterate over each element in the input tuple
4.Append the element to both the maximum and minimum lists
5.Sort the maximum list in descending order and the minimum list in ascending order
6.If the length of the maximum or minimum list is greater than K, remove the last element
7.Print the maximum and minimum K elements
Python3
tup = (5, 20, 3, 7, 6, 8)
k = 2
max_k_elements = []
min_k_elements = []
for elem in tup:
max_k_elements.append(elem)
max_k_elements.sort(reverse=True)
if len(max_k_elements) > k:
max_k_elements.pop()
min_k_elements.append(elem)
min_k_elements.sort()
if len(min_k_elements) > k:
min_k_elements.pop()
print("Maximum", k, "elements:", max_k_elements)
print("Minimum", k, "elements:", min_k_elements)
Maximum 2 elements: [20, 8] Minimum 2 elements: [3, 5]
time Space : O(kn)
Auxiliary Space : O(k)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4