solution 40b --
Now O(log n) in ops and O(1) in memory