Create a struct or class to track the factors and the number of times each factor divides a value in the input array:
1 2 3 4
|
struct Factors {
unsigned factor;
unsigned count;
};
|
Now go through the input array and populate a collection of these. For the sample, I think you'd end up with:
{2,2} {3,4}, {4,1} {5,3} {15,1} {25,1}
Side note: Because so many problems from programming sites deal with prime numbers and factors, you should save your code that handles these. I have a little library that I use for Project Euler problems.
Side side note: Project Euler problems are much better than CodeChef's. They are very well thought out and described.
Back to the problem at hand....
IMPORTANT: for fast access during this part, you want to lookup the Factors by factor.
Now go through your collection from the factor with the highest count to the one with the lowest. When you find one whose factor is in the input array, you have the answer.
IMPORTANT: for fast access in this part, you want to lookup the Factors by count and the input array values by value.
And therein lies the confusing part of this problem. In most computer science problems, you want to lookup items in a collection by one "key". In this case, you lookup factors by one key in the first part (factor) and a different key in the second part (count).