<p>I feel that it should be something very simple and obvious but just stuck on this for the last half an hour and can't move on.</p>

<p>All I need is to split an array of elements into N groups based on element index.</p>

<p>For example we have an array of 30 elements [e1,e2,...e30], that has to be divided into N=3 groups like this:</p>

<pre><code>group1: [e1, ..., e10]
group2: [e11, ..., e20]
group3: [e21, ..., e30]
</code></pre>

<p>I came up with nasty mess like this for N=3 (pseudo language, I left multiplication on 0 and 1 just for clarification):</p>

<pre><code>for(i=0;i&lt;array_size;i++) {
   if(i&gt;=0*(array_size/3) &amp;&amp; i&lt;1*(array_size/3) {
      print "group1";
   } else if(i&gt;=1*(array_size/3) &amp;&amp; i&lt;2*(array_size/3) {
      print "group2";
   } else if(i&gt;=2*(array_size/3) &amp;&amp; i&lt;3*(array_size/3)
      print "group3";
   }
}
</code></pre>

<p>But what would be the proper general solution?</p>

<p>Thanks.</p>

```
```
const int g = 3;                      // number of groups
 const int n = (array_size + g - 1)/g; // elements per group

 for (i=0,j=1; i<array_size; ++i) {
    if (i > j*n)
        ++j;
     printf("Group %d\n", j);
 }
```
```