arrayssumzigzag

Sum of array in a different way


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.


Solution

  • 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