Originally Posted by
iMalc
You're not thinking about the clever way to solve the problem. You only need to store 5 characters at once in memory.
As you read in a char, you place it in a circular buffer. You then update a running product that is divided by the number coming out and multiplied by the number going into the buffer. If this product exceeds your current maximum product, update the maximum.
E.g. your buffer may initially hold 31415, and your running product is therefore 3x1x4x1x5 = 60.
Your next digit is say 9, and the circular buffer head is at position zero. So you take your 60, divide it by 3, giving 20, the multiply that by 9 giving 180 and replace the number in the buffer, giving 91415. You replace your max total with the 180. You'll notice that 9x1x4x1x5 correctly matches the updated running product but that you don't have to multiply all 5 numbers together each time.
Next number is say 2 and your circular buffer head is at position 1. You divide the 180 by 1 and multiply it by 2, giving 360. The buffer now holds 92415 and your max is 360