medium algorithm