While the algorithm provided by @JasonL is suitable for answering the question, but I think that only because the length of the elements lies in the range [1,1000], we do not have to start from 1 and go all the way to 1000.
Take your example, for example:
lengths = [26,103, 59]
An ideal situation would be if the smallest of these numbers, i.e. 26 is also a factor of 103 and 59. We do not have to spend any length and get the maximum profit.
So, in your algorithm you have to do the first check. Now, if the smallest number does not divide the other two numbers. Just scroll the highest number to 1. As user user user user user user user user user user user user user user user user user user user user user user user user user user user user Categories Domain Names E-Marketing Cars Self Portrait Pregnancy Improvement and Delivery Business Blogs Web Design Webmasters Web
So, in your case, instead of checking with [1,1000], if you just check with [1,103] and find the largest multiple of these numbers less than or equal to 26,103, 59, and calculate the profit accordingly. You should have maximum profit.
The time complexity of this algorithm is → O(max(lengths)*size(lengths))
, where lengths
is an array [26,103, 59]
, and max()
is the largest element of this array, and size()
is the length of this array.
Hope you start in the right direction.
source share