class Solution {
public int pivotIndex(int[] nums) {
// Check for null or empty array
if (nums == null || nums.length == 0) {
return -1;
}
// Initialize variables
int leftsum = 0, sum = 0;
for (int i = 0; i sum += nums[i];
}
// Iterate through the array to find the pivot index
for (int i = 0; i if (i != 0) leftsum += nums[i - 1];
if ((sum - leftsum - nums[i]) == leftsum) return i;
}
return -1;
}
}