-1

Let assume I have an MxN array and I should sum this array like in image as shown below;

Array sum

I should sum all x's , all y's , all z's and all g's. I need an approach. My_Brain.exe has stopped and I couldn't find any approach to solve this problem.

Cello
  • 7
  • 5
  • Ok, take a break. Take a walk, read a book, take a nap, have sex, do something else. Then come back to the problem and come up with an approach. _THEN_ come back here. – Erick G. Hagstrom Jan 31 '16 at 17:00

1 Answers1

0
SumColumnZigZag(column) {
    Sum = 0
    for row = 0; row < rows; row++ {
        sum += array[row][column + (row % 2)*(1 - 2*(column % 2))]
    }
    return sum
}

% is the modulo operator

Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46