Is it good programming to pre-compute the values or store(initialize) the values in the array initially and use them ??

obviously storing the values and calling them will take less time… but is it good programming to do so ??

I mean in major competitions will judges welcome such an approach over the ‘pre-computing’ approach ?


I guess that will depend on the problem itself and on the memory limit allocated for such problem…

Usually, questions regarding prime numbers, or Catalan numbers (to count for example, the number of ways of putting parenthesis on a given expression), then sometimes, to do some precomputation and storing the numbers to create some sort of “look-up table”, might be a very effective approach…

To decide when it is wise to apply such technique or not, will depend on your experience!

Best regards,


Talking in a competition scenario, it is your fight and you have to win so you will want the best method, and for that few users even copy paste the array itself for some problems you can see here also.

With respect to good programming practice, it is always better to know the idea and then pre-compute or use the values in the way you want. And, just copy pasting an array is not a good programming practice, generally. As @kuruma said, in questions involving prime numbers, catalan numbers, it is better to pre-compute, but again if the given domain in the problem is small enough many users tend to copy paste an array.

For example, this problem CLMBSTRS of Feb Challenge had good test cases so copy pasting dint work, but pre-computing did. My Solution with copy pasted array as comment.
This with pre-computing.