Imagine, you have a big array A, which is mostly zeroes, but contains a short subarray B which has only strictly positive entries. For instance:

|              A              |
[0   0   0   0   1   2   3   0]
               |     B     | 

Now say, we split the array A into consecutive subarrays of length d (with a shorter final array, if the d doesn't divide the length of A). For d = 3:

[0   0   0   0   1   2   3   0]
           |           |
[0   0   0] [0   1   2] [3   0]

And now sum up each of those arrays:

[0 3 3]

And discard zero sums:

[3 3]

The Challenge

You're to write a function which prints that final array of sums in any convenient array format, given the following 5 parameters (or any subset thereof, if you don't need all of them):

  • The small array B.
  • The size of the small array B (3 in the above example).
  • The size of the big array A (8 in the above example).
  • The index where the small array begins (4 in the above example).
  • The length d of the subarrays (3 in the above example).

The Rules

  • You are not allowed to construct the big array A explicitly!
  • You are not allowed to construct any of the subarrays of length d explicitly!
  • That means you have to get the computation directly from the B and the other parameters at your disposal!

Fewest characters wins!

  • \$\begingroup\$ Can we remove/omit the inputs we don't need? As far as I can tell, I don't need size_bigarray or size_smallarray at all. \$\endgroup\$
    – Geobits
    Nov 24, 2014 at 16:00
  • \$\begingroup\$ yes if you don't need it \$\endgroup\$
    – nvidia
    Nov 24, 2014 at 16:02
  • \$\begingroup\$ May we construct the mini arrays that overlap with small_array? In your example, that would only be [0,1,2], 3,0]. Or none at all? \$\endgroup\$ Nov 24, 2014 at 16:09
  • \$\begingroup\$ no mini arrays should be constructed \$\endgroup\$
    – nvidia
    Nov 24, 2014 at 16:14
  • \$\begingroup\$ I tried to reword the spec a bit, to make the actual problem a bit more visual in the first place. Feel free to roll back the edit if you don't like it, it's just a suggestion! \$\endgroup\$ Nov 25, 2014 at 1:48

4 Answers 4


CJam, 34 32 28 characters


This is a compressed form of the following 31 bytes solution


This can be golfed further.

Try it online here

Input is in format of [small_array] index_where_small_array_begins divisor

For example:

[1 2 3] 5 3{"ɚ광䃻噣攍圍숵셳ﶓ訙➰鑓쥾"2G#b126b:c~}~

will give output

[1 5]

Explanation (outdated):

{                                   "Begin of function block";
 :D                                 "Store the divisor in D";
   %                                "Calculate index_where_small_array_begins%divisor";
    0a*                             "Get an array of 0 on length above number";
       \+                           "Prepend it to the small_array";
         _,{            }*          "Run this code block length(small_array) times";
            0\                      "Put 0 before small_array. This 0 marks the"
                                    "current sum of the current mini_array";
              {      }D*            "Run this code block divisor times";
               0+                   "Add 0 at the end of mini array";
                 (                  "Take out the first element from the mini array";
                  @                 "Rotate the stack to get current sum on top";
                   +                "Add the first element to the current sum";
                    \               "Swap to put current mini_array on top of stack";
                          ;]        "Pop the residual array and wrap the sums in an array";
                            0-p     "Remove 0 and print the sum_of_mini_arrays_array";
                               }    "End of function block";

Pyth - 30 32

Edit: Fixed an error with mini array size 1 or very large offset.


Defines a function : that takes small_array, index_where_small_array_begins and divisor as arguments. You can call it like:

:[1 2 3) 5 3

Returns: [1, 5]

Try it online


D:GHY                            : Define :
     Km0G                        : Make K an array of len(G) zeroes
         FNG             )       : for N in small_array
            ~@K%/HYlGN           : Add N to K[floor_div(H,Y)%len(G)]
                      ~H1        : Increment H
                          RfTK   : Return K without any zeroes
  • \$\begingroup\$ You can change *]0lG to m0G. \$\endgroup\$
    – isaacg
    Nov 25, 2014 at 3:42
  • \$\begingroup\$ @isaacg Thanks, that's good to know :) \$\endgroup\$ Nov 25, 2014 at 4:41
  • \$\begingroup\$ @isaacg I've also noticed in a few recent problems that pyth kinda loses out in some golf problems because it doesn't automatically mod indexing by the length of the collection. I looked at the source code, and this doesn't look easy to change, but it might be a good idea ;) (you might also then be able to overload @ with something else... ;p ) \$\endgroup\$ Nov 25, 2014 at 4:46
  • \$\begingroup\$ The only issue would be that assigning to a list could no longer use the same character as a lookup. I'll take a look. \$\endgroup\$
    – isaacg
    Nov 25, 2014 at 5:11

CJam, 30 28 characters


(SE may have swallowed some unprintable characters. Please copy the code from this character counter.)

Yay for counting by characters and Unicode packing. The above string is converted to character codes, interpreted as base-216, converted to base-129, and converted back to characters, and evaluated, to give the actual code (measuring 30 bytes):


This is a block, the closest thing to a function in CJam. It expects the small array, the offset, and the divisor on the stack. You can use it like

[1 2 3]4 3{"    ড숺ꤪ洽ὦ﮻ڤ綃掖迓湯"2G#b129b:c~}~

Test it here.


:M                           "Store divisor in M.";
  %                          "Take offset modulo divisor to get R.";
   0a                        "Push [0].";
     *                       "Repeat to get R zeros.";
      \+                     "Preprend to small array.";
        {              }h    "Repeat while... leaves condition on the stack.";
         0\                  "Put a 0 underneath the array to initialise sum.";
           {        }M*      "Run the block M times.";
            _0a?             "Replace the list by [0] if it's empty.";
                (            "Split off first element.";
                 @           "Pull up current sum.";
                  +\         "And and pull list back to the top.";
                         ;   "Remove leftover empty list.";
                          ]  "Wrap sums in array.";
                           p "Print string representation to STDOUT."; 

JavaScript (ES6) 58

Function with 3 parameters
- b is the small array
- i is the index where the small array begins
- d is the dimension of the subarrays

(t is used just as a local variable)


Test In FireFox/FireBug console



[ 3, 3 ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.