educative.io

Educative

Can we verify the while condition?

The solution says that the while condition is: while (low <= high)
but shouldn’t it be while (low < high) ?

Can you please verify the correct while condition? Thanks.

Hey!

You can imagine low and high as pointers and when they both are equal they are both pointing towards one number. This number is also the last number to be added to the squared list, if we use while (low < high) that number won’t get added to the zeros list.

As it is the last number to be added both squares will be equal, and the right(same as left) index will be simply squared and added to the smallest index.

Try running this in the same widget and remove the = sign from the while condition.

print("Squares: " + str(make_squares([2, 6, 7, 8, 9])))

The last index would end up staying zero like: Squares: [0, 36, 49, 64, 81]

1 Like