We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Have a question folks. I have solved this in a FP approach with immutable data structures. However, TC #5 onwards am hitting an OOM error.

My algo is very simple - use the scala set.subsets method to generate combinations of (1 to numberOfFriends) and then apply the given formula for each of these subsets, get the max of the applied formula.

The timeout is because the subsets and the further computation takes into account all the combinations. For ex: If a subset of length 3 achieves the maximum possible number of mangoes, the computation should stop there and not go ahead with subsets of lengths > 3. This is not the case here. What I'm trying to find out is how to do this without using a loop or in a pure functional approach. Can someone provide any clues ? I can message my Scala code if needed.

For stopping early, you can use a recursive function, that stops when the max number of mangoes is reached. I also made the solution in scala and ran into the timeouts as well. Unfortunately this will not solve your timeouts.

I made two improvements in the calculation to get rid of the timeouts. I can give some hints:
1. It is not strictly neccesarry to consider all combinations
2. You don't necessarily have to start with 1 invitation first.

## Mangoes

You are viewing a single comment's thread. Return to all comments →

Have a question folks. I have solved this in a FP approach with immutable data structures. However, TC #5 onwards am hitting an OOM error.

My algo is very simple - use the scala set.subsets method to generate combinations of (1 to numberOfFriends) and then apply the given formula for each of these subsets, get the max of the applied formula.

The timeout is because the subsets and the further computation takes into account all the combinations. For ex: If a subset of length 3 achieves the maximum possible number of mangoes, the computation should stop there and not go ahead with subsets of lengths > 3. This is not the case here. What I'm trying to find out is how to do this without using a loop or in a pure functional approach. Can someone provide any clues ? I can message my Scala code if needed.

For stopping early, you can use a recursive function, that stops when the max number of mangoes is reached. I also made the solution in scala and ran into the timeouts as well. Unfortunately this will not solve your timeouts.

I made two improvements in the calculation to get rid of the timeouts. I can give some hints: 1. It is not strictly neccesarry to consider all combinations 2. You don't necessarily have to start with 1 invitation first.

Good luck!