How to find two numbers from a list that are just smaller and just greater than N in Python

1 Answer

0 votes
import sys

arr = [3, 8, 1, 9, 4, 10, 7, 13, 2, 6]
size = len(arr)
N = 4

just_greater = sys.maxsize
just_smaller = -sys.maxsize

i = 0
while (i < size) :
    if (arr[i] > N and arr[i] < just_greater) :
        just_greater = arr[i]
    if (arr[i] < N and just_smaller < arr[i]) :
        just_smaller = arr[i]
    i += 1
    
print("just_smaller: " + str(just_smaller))
print("N: " + str(N))
print("just_greater: " + str(just_greater))
    
   
    
'''
run:

just_smaller: 3
N: 4
just_greater: 6

'''

 



answered Sep 19, 2022 by avibootz
...