How to find the pivot index in C++
The pivot index represents the position within an array where the cumulative sum of elements preceding it matches the cumulative sum of elements succeeding it. The specific value at the pivot index doesn’t affect this computation, and any array index can be the pivot index.
Here’s an illustration to understand the pivot index better.
In the illustration above, the 5th index serves as the pivot because the cumulative sum of the elements preceding the 5th index equals 13, and similarly, the sum of the elements succeeding it also amounts to 13.
Note: When all the elements succeeding it add up to 0, Index 0 is the pivot index. When all the elements preceding it add up to 0, the last index is the pivot index.
Code example
The following code shows how to find the pivot index of an array using C++:
#include <iostream>using namespace std;int main() {int arr[] = {1,4,6,8,8,5,5,10,13,9}; // Index 6 is the answer// Following for loop is for displaying the array.cout << "The array is : {";for (int i=0; i < sizeof(arr) / sizeof(int); i++){if (i+1 != sizeof(arr) / sizeof(int))cout << arr[i] << " , ";elsecout << arr[i] << "}\n";}// Following loop calculates the total sum of the array.int sum = 0;for (int i=0; i < sizeof(arr) / sizeof(int) ; i++){sum = sum + arr[i];}// Following code finds the pivot index, if it exists.int total = 0;bool flag = true;for (int i=0; i < sizeof(arr) / sizeof(int); i++){sum = sum - arr[i];if (total == sum){flag = false;cout << "Index " << i << " is pivot" << endl;}total = total + arr[i];}// The following is displayed if no pivot index is found.if (flag){cout << "There is no pivot index" << endl;}return 0;}
Explanation
Line 6: We initialize an array with the name
arrwith its pivot index commented out ahead.Lines 9–15: We run a
forloop for displaying thearrarray.Line 18: We initialize an integer variable
sumas0.Lines 19–21: We run another
forloop, which calculates the sum of all elements of the array and stores this value insum.Line 25: We initialize an integer variable
totalas0. Later, we’ll iteratively add each element of the array intotaland compare it withsumto find the pivot index.Line 26: We initialize a boolean variable
flagastrue. We’ll use this variable to check whether the pivot index has been found. If the pivot index is found, we change the value offlagtofalse.Lines 27–34: We run another
forloop whereiis initialized as0, and goes up to the last index of the array. In each iteration of this loop, we first subtract the value of the element at the index numberifromsum, and then compare that with the value oftotal. After the comparison, we add the value of this element tototal.During the comparison, neither the variable
sum, nortotalcontains the value of the element at the indexi.During the comparison,
sumis the sum of all elements at the indexes succeeding the indexi, whiletotalis the sum of all elements at the indexes preceeding the indexi.When
iis0, the value oftotalis0while the value ofsumis equal to the sum of all elements in the array, except the first one. Wheniis the last index of the array, the value ofsumis0, whiletotalis equal to the sum of all elements in the array, except the last one.If, at any iteration, the comparison of
sumandtotalproves them equal,flagbecomesfalseand the loop stops.
Lines 37–39: If
flagis stilltrue, it means thatsumandtotalnever became equal in value i.e., no pivot index was found. In that case, a message (There is no pivot index) is displayed to the user stating thatarrdoesn’t contain a pivot index.
Knowledge test
In the following quiz, match the arrays given on the left side with their pivot indexes on the right side.
{10, 12, 8, 2}
No pivot index
{5, 4, 4, 4, 4, 4, 4, 4, 5}
Index 1
{4, 10, -5, 6, -10, -6, -6, 7, 2}
Index 0
{100, 4, 4, -8, -9, 5, 4}
Index 8
{117, 35, 22, 24, 15, -120}
Index 4
Conclusion:
Initially, it might seem complex to find the pivot index in an array using a C++ algorithm, but the more we understand the topic, the simpler the coding becomes. In this answer, we understood the concept using the illustration and practiced identifying the pivot index, while also learning, in detail, how our solution code works step by step.
Free Resources