Find the “Kth” max and min element of an array

prepinsta-prime

Get over 200+ Courses under One Subscription

mute

Don’t settle Learn from the Best with PrepInsta Prime Subscription

Learn from Top 1%

One Subscription, For Everything

The new cool way of learning and upskilling -

Limitless Learning

One Subscription access everything

Job Assistance

Get Access to PrepInsta Prime

Top Faculty

from FAANG/IITs/TOP MNC's

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

6 comments on “Find the “Kth” max and min element of an array”


  • nandan767644

    def kth_min_max(arr, k):
    arr.sort()
    kth_min = arr[k – 1]
    kth_max = arr[-k]
    return kth_min, kth_max

    # Example
    arr = [7, 10, 4, 3, 20, 15]
    k = 3
    kmin, kmax = kth_min_max(arr, k)
    print(“Kth smallest:”, kmin)
    print(“Kth largest:”, kmax)


  • rohitraj4513

    #Find the “Kth” max and min element of an array
    arr = [ 2 , 4 , 18 , 77 , 22 , -89 , 18 , 88 , 336]
    low = 10**9
    mx = -10**9
    for i in range(len(arr)):
    if arr[i] mx :
    mx = arr[i]
    print(f”Max element in Array : {mx}”)
    print(f”min element in Array : {low}”)


  • Prayansh

    l=[10,56,20,48,21,458,-1,0,569]
    n=len(l)
    while True:
    k=int(input(“Enter k: “))
    if k<=n:
    break
    else:
    print("Wrong value enter again: ")

    l1=sorted(l)
    min=l1[k-1]
    max=l1[n-k]

    print(min,max)


  • nitesh

    def minman(arr,k):
    arr.sort()
    max=arr[-k]
    min=arr[k-1]
    return min,max

    arr=[1,25,38,43,54,68,72,28,19,12]
    k=2
    s,d=minman(arr,3)
    print(s)
    print(d)