for use in Algorithm 1 on Coursera