Open In App

Python | Find smallest element greater than K

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This problem involves searching through a list to identify the smallest number that is still larger than K. We will explore different methods to achieve this in Python In this article, we’ll look at simple ways to find the smallest element greater than k in a list using Python.

Binary search on a sorted array helps find smallest element greater than K by efficiently locating the insertion point. It’s particularly useful for handling multiple queries on the same array after sorting.

Example:

Python
from bisect import bisect_right

# List 'a' and value 'k'
a = [1, 4, 7, 5, 10]
k = 6

# Sort the list for binary search
a.sort()

# bisect_right finds the index where 'k' would fit
# returning the index of the first element k
res = a[bisect_right(a, k)]  # Smallest element greater than 'k'
print(str(res))

Output
7

Explanation:

  • Code starts by importing the bisect_right function from the bisect module, which helps find the insertion point for a given value in a sorted list.
  • It defines a list a and a target value k.
  • list a is sorted to ensure that the binary search can be applied correctly.
  • bisect_right(a, k) is used to find the index where k would fit in the sorted list a. This index points to the first element greater than k.

Using sorted Set allows maintaining a sorted collection while enabling efficient insertion and querying. This method uses a binary search to quickly find the smallest element greater than K in dynamic datasets.

Example:

Python
from sortedcontainers import SortedSet

# List 'a' and value 'k'
a = [1, 4, 7, 5, 10]
k = 6

# Create SortedSet to keep elements sorted
s = SortedSet(a)

# bisect_right finds the index of the smallest element greater than 'k'
res = s.bisect_right(k)

# Return the element at the found index, or None if no such element exists
result = s[res] if res < len(s) else None
print(result)

Output:

7

Explanation:

  • Code imports SortedSet from the sortedcontainers module. This class automatically keeps the elements sorted as they are added or removed.
  • A list a containing integers and a value k are defined. The goal is to find the smallest element in a that is greater than k.
  • List a is converted into a SortedSet named s, which will maintain the elements in sorted order.
  • Method s.bisect_right(k) finds the index of the smallest element in s that is greater than k. This method is similar to a binary search.

Linear search involves scanning through the list to find the smallest element greater than K .It’s a simple and straightforward approach, suitable for small arrays or when sorting isn’t necessary.

Example:

Python
# List 'a' and value 'k'
a = [1, 4, 7, 5, 10]
k = 6

# Initialize 'min_val' to infinity
min_val = float('inf')

# Find the smallest element greater than 'k'
for n in a:
    if n > k < min_val:
        min_val = n

# Return result: smallest number greater than 'k', or None if not found
res = min_val if min_val != float('inf') else None
print(res)

Output
7

Explanation:

  • The variable min_val is initialized to infinity (float(‘inf’)), which acts as a placeholder to ensure that any element in the list will be smaller and can be compared against it.
  • A for loop iterates through each element (num) in the list a. If num is greater than k and smaller than the current min_val, it updates min_val to that element.


Next Article
Practice Tags :

Similar Reads