@mughat: The optimal solution