implement gmin and gmax and gmin2 on the paper