implement the code from #4488 by @k-akarsh.

Implemented the min and max inspired by the work done by @k-akarsh in #4488

Closes #4488