GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Arrays/find_el_where_k_greater_or_equal.py at master · onowdev/coding-problems

Expand file tree

/

find_el_where_k_greater_or_equal.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
49 lines (34 loc) · 1.03 KB
/

find_el_where_k_greater_or_equal.py

Copy path

File metadata and controls

49 lines (34 loc) · 1.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
Find an Element Which is Smaller or Equal to Exactly K Numbers
You have to find some number X greater than 0 where exactly K elements in that list are greater than or equal to the number X.
If there are multiple such values return the smallest possible.
If there is no such X, return (-1).
Input: [3,8,5,1,10,3,20,24], 2
Output: 11
Output explanation: Only 20 and 24 are greater or smaller from 11 (11 is the smallest solution, also 12, 13...20 are solutions).
=========================================
Sort the array and check the Kth element from the end.
Time Complexity: O(NLogN)
Space Complexity: O(1)
'''
############
# Solution #
############
def get_minimum_X(arr, k):
n = len(arr)
if n == 0 or k > n:
return -1
if k == n:
return 1
arr.sort()
if k == 0:
return arr[-1] + 1
if arr[-k] == arr[-(k + 1)]:
return -1
return arr[-(k + 1)] + 1
###########
# Testing #
###########
# Test 1
# Correct result => 11
print(get_minimum_X([3, 8, 5, 1, 10, 3, 20, 24], 2))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.