/*Write a Program to take an array of integers as input, calculate the pivot index of this array. The pivot index is
the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers
strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there
are no elements to the left. This also applies to the right edge of the array. Print the leftmost pivot index. If no
such index exists, print -1.*/
#include <stdio.h>
int main() {
int n;
int arr[n];
for (int i
= 0; i
< n
; i
++) scanf("%d", &arr
[i
]);
int total = 0;
for (int i = 0; i < n; i++) total += arr[i];
int leftSum = 0, pivot = -1;
for (int i = 0; i < n; i++) {
int rightSum = total - leftSum - arr[i];
if (leftSum == rightSum) {
pivot = i;
break;
}
leftSum += arr[i];
}
return 0;
}
Ci8qV3JpdGUgYSBQcm9ncmFtIHRvIHRha2UgYW4gYXJyYXkgb2YgaW50ZWdlcnMgYXMgaW5wdXQsIGNhbGN1bGF0ZSB0aGUgcGl2b3QgaW5kZXggb2YgdGhpcyBhcnJheS4gVGhlIHBpdm90IGluZGV4IGlzIAp0aGUgaW5kZXggd2hlcmUgdGhlIHN1bSBvZiBhbGwgdGhlIG51bWJlcnMgc3RyaWN0bHkgdG8gdGhlIGxlZnQgb2YgdGhlIGluZGV4IGlzIGVxdWFsIHRvIHRoZSBzdW0gb2YgYWxsIHRoZSBudW1iZXJzCnN0cmljdGx5IHRvIHRoZSBpbmRleCdzIHJpZ2h0LiBJZiB0aGUgaW5kZXggaXMgb24gdGhlIGxlZnQgZWRnZSBvZiB0aGUgYXJyYXksIHRoZW4gdGhlIGxlZnQgc3VtIGlzIDAgYmVjYXVzZSB0aGVyZSAKYXJlIG5vIGVsZW1lbnRzIHRvIHRoZSBsZWZ0LiBUaGlzIGFsc28gYXBwbGllcyB0byB0aGUgcmlnaHQgZWRnZSBvZiB0aGUgYXJyYXkuIFByaW50IHRoZSBsZWZ0bW9zdCBwaXZvdCBpbmRleC4gSWYgbm8Kc3VjaCBpbmRleCBleGlzdHMsIHByaW50IC0xLiovCiNpbmNsdWRlIDxzdGRpby5oPgoKaW50IG1haW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