educative.io

More succint solution

Can you please check if my solution is correct and thus more succinct?
def max_min(lst):

i = 0

ptr = 0

n = len(lst)

big_swap = True

b = n - 1

s = n

i = 0

if n == 0:

    return lst

while s > i:

    if big_swap:

        lst[i],lst[b] = lst[b],lst[i]

        b -= 1

    else:

        if s == n:

            lst[i],lst[s-1] = lst[s-1],lst[i]

        else:

            lst[i],lst[s] = lst[s],lst[i]

        s -= 1

    big_swap = not big_swap

    i += 1        

return lst

Hi @Mushfique_Ahmed,

This is a good way to go about it. I have checked on various edge cases and it works. Well done!
The time complexity is O(n) as the entire array is to be traversed.

Happy learning at Educative!
Anum Hassan|Developer Advocate
Educative.io

1 Like