Skip to content

Commit 89a7291

Browse files
Update kth_largest_element.py
1 parent a71618f commit 89a7291

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

data_structures/arrays/kth_largest_element.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ def kth_largest_element(arr, position):
9999
raise ValueError("Invalid value of 'position'")
100100
low, high = 0, len(arr) - 1
101101
while low <= high:
102-
if low > len(arr) - 1 or high < 0:
103-
return -1
104102
pivot_index = partition(arr, low, high)
105-
if pivot_index == position - 1:
103+
target_index = position - 1
104+
if pivot_index == target_index:
106105
return arr[pivot_index]
107-
elif pivot_index > position - 1:
106+
elif pivot_index > target_index:
108107
high = pivot_index - 1
109108
else:
110109
low = pivot_index + 1

0 commit comments

Comments
 (0)