Garey

Solution -- Hackerrank

Nov 16th, 2020 (edited)
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <numeric>
  3.  
  4. auto balanceIndex(const int array[], const int count) -> int {
  5.    auto rightPartition = std::accumulate(array + 1, array + count, 0);
  6.    auto leftPartition = 0;
  7.    
  8.    for(auto index = 0; (index + 1) < count; index++) {
  9.       rightPartition -= array[index + 1];
  10.       leftPartition  += array[index];
  11.      
  12.       if(leftPartition == rightPartition)
  13.         return (index + 1);
  14.    }
  15.    
  16.    return -1;
  17. }
  18.  
  19. auto main() -> int {
  20.     auto count(0);
  21.     std::cin >> count;
  22.    
  23.     auto array = new int[count];
  24.    
  25.     for(auto index = 0; index < count; index++)
  26.         std::cin >> array[index];
  27.    
  28.     std::cout << balanceIndex(array, count);
  29. }
Add Comment
Please, Sign In to add comment