If you are looking for a sum s, and the total of all array elements is t, then the sum of the first i and the last j elements must be t - s, so the elements from index i to index n-j exclusive add up to s.
So you create a hash table or a balanced binary tree for the sum of the last j elements, starting with j = 0, then adding another element for j = 1, 2, 3 etc. Hash table will be O (1) on average, but balanced binary tree is O (log n) worst case.
For each j, you check whether t minus s minus the sum of the first i = n-j elements is in the hash table or binary tree, and proceed until j = n and i = 0.
O (n) average using a hash table, but no guarantee for the worst case. O (n log n) if you use a balanced search tree.
As an example with the data [1, -3, 4, 8, 2, -14, 3, -1, 10, 6]: The total is t = 16, we were given s = 9, so the sum of the first i and the last j elements must be 7. The list of sums of the last j elements are:
j = 0: 0
j = 1: 0, 6
j = 2: 0, 6, 16
j = 3: 0, 6, 16, 15
j = 4: 0, 6, 16, 15, 18
j = 5: 0, 6, 16, 15, 18, 4
j = 6: 0, 6, 16, 15, 18, 4, 6
j = 7: 0, 6, 16, 15, 18, 4, 6, 14
j = 8: 0, 6, 16, 15, 18, 4, 6, 14, 18
j = 9: 0, 6, 16, 15, 18, 4, 6, 14, 18, 15
j = 10: 0, 6, 16, 15, 18, 4, 6, 14, 18, 15, 16
Then we calculate the sum of the first i = 10-j elements, and 7 minus that sum must be in the last of the sums of the last j elements.
j = 0, i = 10: 7 - 16 = -9, not found
j = 1, i = 9: 7 - 10 = -3, not found
j = 2, i = 8: 7 - 0 = 7, not found
j = 3, i = 7: 7 - 1 = 6, found
j = 4, i = 6: 7 - (-2) = 9, not found
j = 5, i = 5: 7 - 12 = -5, not found
j = 6, i = 4: 7 - 10 = -3, not found
j = 7, i = 3: 7 - 2 = 5, not found
j = 8, i = 2: 7 - (-2) = 9, not found
j = 9, i = 1: 7 - 1 = 6, found
j = 10, i = 0: 7 - 0 = 7, not found
The first 1 elements have a sum of 1, the last 1 or 6 elements have a sum of 6, so elements 1..9 or 1..4 have a sum of 9. The time is basically n insertions and lookups into a data structure.