I gave the problem a bit more thought. The statement in my last post "the max pain is achieved when the gain from the calls equals to the gain from the puts" is incorrect. This is the cute pain.
The max pain should minimize the total gains for both calls and puts, that is, in the amat example, minimize
total gains = 2753 x (mp - 17.5) + 32342 x (mp - 20) + 12913 x (22.5 - mp) + 2009 x (25 - mp)
In this case, 2753 + 32342 - 12913 - 2009 = 20173 > 0
Thus, the max pain should be 20, the lower bound of the range [20, 22.5]. If the number is less than 0 (in other words, the total number of in-the-money calls is less than the total number of in-the-money puts), you choose 22.5, the upper bound of the range [20, 22.5] for the max pain.
Sorry for the confusion. Actually, there is no need for division. I learned something today.