### Get the sum of only a specific group of elements in a vector

Hello,
I have a sorted vector and my operation is to get the sum, but only for the group of numbers that have more than 4 elements and difference less than 3.

ex: Let say I have vec1={0 ,4,6,7,8,9 , 20,21,22,23,24,25 ,50,51,52 ,70,71,72,73,74,75}

My output must be
group 1: 4,6,7,8,9 Sum: 34
group 2: 20,21,22,23,24,25 Sum: 135
group 3: 70,71,72,73,74,75 Sum: 435

Below is my attempt where i am missing the last element of each group and also the last group.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include using namespace std; int main() { std::vectorvec1{0,4,6,7,8,9,20,21,22,23,24,25,50,51,70,71,72,73,74,75}; std::vectorvec2; int counter1 =0; int counter2 =0; int counter3 =0; for (int a =0; a< vec1.size()-1; a++) { if ((vec1[a+1]-vec1[a])<3) { counter1++; vec2.push_back(vec1[a]); } else { if (counter1>=4) { int sum=0; for(int b =0; b4)) { counter1++; vec2.push_back(vec1[a]); } } cout<

Last edited on
 ``12345678910111213`` ``````std::vector::iterator begin = vec1.begin(); std::advance(begin, 1); std::vector::iterator end = begin; std::advance(end, 4); int sum1 = std::accumulate(begin, end, 0); begin = vec1.begin(); std::advance(begin, 5); end = begin; std::advance(end, 4); int sum2 = std::accumulate(begin, end, 0); // ... and so on. ``````
Last edited on
Thank you very much for the response. This seems to work only if i know the the position of the elements in the vector.

I have 1.8 million such vectors cumming in a loop and they are all distinct from each other. The number of groups in the vector is not certain. I might have any number of such groups in the vector and I want to do it for all the groups. The point is that the number of such "sum's" to be calculated is not certain
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include using namespace std; const int minNumber = 4 + 1; const int maxDifference = 3 - 1; void findGroups( vector V ) { if ( V.size() == 0 ) return; int currentNumber = 0; int currentSum = 0; int numGroups = 0; int last = V[0] - maxDifference - 1; vector group; for ( int e : V ) { if ( e - last <= maxDifference ) // Continue with current group { currentSum += e; currentNumber++; group.push_back( e ); } else // Finish previous group and start anew { if ( currentNumber >= minNumber ) // Previous was a valid group { numGroups++; cout << "Group " << numGroups << ": " << group[0]; for ( int g = 1; g < group.size(); g++ ) cout << ", " << group[g]; cout << " Sum: " << currentSum << '\n'; } group.clear(); group.push_back( e ); currentSum = e; // Start afresh currentNumber = 1; } last = e; } // Deal with leftovers at the end if ( currentNumber >= minNumber ) // Previous was a valid group { numGroups++; cout << "Group " << numGroups << ": " << group[0]; for ( int g = 1; g < group.size(); g++ ) cout << ", " << group[g]; cout << " Sum: " << currentSum << '\n'; } } int main() { vector V = {0,4,6,7,8,9,20,21,22,23,24,25,50,51,70,71,72,73,74,75}; findGroups( V ); }``````

 ```Group 1: 4, 6, 7, 8, 9 Sum: 34 Group 2: 20, 21, 22, 23, 24, 25 Sum: 135 Group 3: 70, 71, 72, 73, 74, 75 Sum: 435```
Last edited on
This is brilliant. I am extremely thankful for the time and interest you took to help me with the problem.
Topic archived. No new replies allowed.