Python - Maximum element till K value
Last Updated :
17 Apr, 2023
One of the problem that is basically a subproblem for many complex problems, finding maximum number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this problem can be solved is using a loop and just max the occurrences of elements that are till given number K.
Python3
# Python 3 code to demonstrate
# Maximum element till K value
# using naive method
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
# initializing k
k = 4
# printing list
print ("The list : " + str(test_list))
# using naive method
# Maximum element till K value
res = 0
for i in test_list :
if i <= k :
res = max(res, i)
# printing the intersection
print ("The maximum number till K : " + str(res))
Output : The list : [1, 7, 5, 6, 3, 8]
The maximum number till K : 3
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using list comprehension This method achieves this task in a similar way, but in a more concise manner. List comprehension always lowers the lines of codes in the program even though runs a similar approach in the background.
Python3
# Python 3 code to demonstrate
# Maximum element till K value
# using list comprehension
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
# initializing k
k = 4
# printing list
print ("The list : " + str(test_list))
# using list comprehension
# Maximum element till K value
res = max([i for i in test_list if i <= k])
# printing the intersection
print ("The maximum number till K : " + str(res))
Output : The list : [1, 7, 5, 6, 3, 8]
The maximum number till K : 3
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(1), constant extra space is required
Method #3 : Using sorted()
Approach
Sort the list in descending order and then take the Kth element from the sorted list.
Step-by-step algorithm
1. Define a function called max_element_till_k that takes two arguments: a list and an integer K.
2. Slice the list to get the first K elements, and store it in a variable called sliced_list.
3. Sort the sliced_list in descending order using the sorted() function with the reverse=True argument, and store it in a variable called sorted_list.
4. Return the first element of the sorted_list using index notation, which will be the maximum element in the list till the K value.
Python3
def max_element_till_k(lst, k):#define list and k
sorted_list = sorted(lst[:k], reverse=True)#sort the list upto k value
return sorted_list[0]# get the maximum element in sorted list
lst = [3, 5, 2, 8, 1, 9, 4]#input
k = 5#k value
max_val = max_element_till_k(lst, k)#max value
print(max_val) #print output
Time complexity: O(klogk), where k is the upper limit of the range in which the maximum number has to be found
Space complexity: O(k), where k is the upper limit of the range in which the maximum number has to be found
Similar Reads
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Maximum product using K elements Sometimes, while working with Python lists, we can have a problem in which we need to maximize certain numbers. There can be many conditions of maximizing. Example of that is maximizing product by using K elements. Lets discuss certain ways in which this can be performed. Method #1 : Using max() + s
3 min read
Python - Maximum and Minimum K elements in Tuple 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
8 min read
Keys with Maximum value - Python In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to ide
4 min read
Python - Maximum frequency in Tuple Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 min read
Key with Maximum Unique Values - Python The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique v
3 min read
Python | Get first element with maximum value in list of tuples In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python - K Maximum elements with Index in List GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Elements Maximum till current index in List Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
7 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read