Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <numeric>
- auto balanceIndex(const int array[], const int count) -> int {
- auto rightPartition = std::accumulate(array + 1, array + count, 0);
- auto leftPartition = 0;
- for(auto index = 0; (index + 1) < count; index++) {
- rightPartition -= array[index + 1];
- leftPartition += array[index];
- if(leftPartition == rightPartition)
- return (index + 1);
- }
- return -1;
- }
- auto main() -> int {
- auto count(0);
- std::cin >> count;
- auto array = new int[count];
- for(auto index = 0; index < count; index++)
- std::cin >> array[index];
- std::cout << balanceIndex(array, count);
- }
Add Comment
Please, Sign In to add comment