using min instead of iterative approach