Open In App

Find the largest multiple of 2, 3 and 5

Last Updated : 21 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An array of size n is given. The array contains digits from 0 to 9. Generate the largest number using the digits in the array such that the number is divisible by 2, 3 and 5. For example, if the arrays is {1, 8, 7, 6, 0}, output must be: 8760. And if the arrays is {7, 7, 7, 6}, output must be: “no number can be formed”. Source: Amazon Interview | Set 7 This problem is a variation of “Find the largest multiple of 3“. Since the number has to be divisible by 2 and 5, it has to have last digit as 0. So if the given array doesn’t contain any zero, then no solution exists. Once a 0 is available, extract 0 from the given array. Only thing left is, the number should be is divisible by 3 and the largest of all. Which has been discussed here

Time Complexity: O(N)

Auxiliary Space: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads