Calculate the amx sub sum of an array in time complexity of O(n)