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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>

using namespace std;

int main()
{
std::vector<int>vec1{0,4,6,7,8,9,20,21,22,23,24,25,50,51,70,71,72,73,74,75};
std::vector<int>vec2;
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; b<vec2.size();b++)
            {
                cout<<vec2[b]<<"\t";
                sum = sum+ vec2[b];
            }
            cout<<"\nCounter "<<counter1<<"\t"<<"Sum "<<sum<<endl;
            counter3++;
        }
        counter1 =0;
        vec2.clear();
        cout<<" DIFFERENCE EXCEEDED "<<endl;
    }
    if (((vec1[a+1]-vec1[a])<3)&&(a==vec1.size()-2)&&(counter1>4))
    {
     counter1++;
     vec2.push_back(vec1[a]);
    }

} 
cout<<counter3<<"\t";
    return 0;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<int>::iterator begin = vec1.begin();
std::advance(begin, 1);
std::vector<int>::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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
using namespace std;

const int minNumber = 4 + 1;
const int maxDifference = 3 - 1;


void findGroups( vector<int> V )
{
   if ( V.size() == 0 ) return;

   int currentNumber = 0;
   int currentSum    = 0;
   int numGroups     = 0;
   int last          = V[0] - maxDifference - 1;
   vector<int> 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<int> 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.
Registered users can post here. Sign in or register to post.