Iterate through the array from index 0 to the end.
a. For each index i, calculate the right sum as S - nums[i] - leftsum.
b. If leftsum equals the right sum, return the current index i as the pivot index.
c. Update leftsum by adding nums[i] to it.
If no pivot index is found during the iteration, return -1.