Adding a data structure to store the intermediate sums

Saving the intermediate sums in an array would greatly increase the processing time as the logic requires calculating the same sum repeatedly