Forming greatest and smallest numbers with given digits