![]() |
VOOZH | about |
Given an array of numbers, the task is to print those numbers in ascending order separated by commas that have 1, 2, and 3 in their digits. If no number containing digits 1, 2, and 3 is present then print -1.
Examples:
Input : numbers[] = {123, 1232, 456, 234, 32145}
Output : 123, 1232, 32145Input : numbers[] = {9821, 627183, 12, 1234}
Output : 1234, 627183Input : numbers[] = {12, 232, 456, 234}
Output : -1
First finding all the number in form of array which contains 1, 2 & 3 then sort the number according to 1, 2 and 3 and then print it.
Follow the steps below to implement the above idea:
Below is the implementation of the above approach:
123, 1232, 32145
Time Complexity: O(N log(N)), where N is the length of the given number.
Auxiliary Space: O(N)
Please suggest if someone has a better solution that is more efficient in terms of space and time.