code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

039 Crossing Sum

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Given a rectangular matrix and integers a and b, consider the union of the ath row and the bth (both 0-based) column of the matrix (i.e. all cells that belong either to the ath row or to the bth column, or to both). Return sum of all elements of that union.

Example

For
matrix = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] a = 1 and b = 3, the output should be crossingSum(matrix, a, b) = 12.

Here (2 + 2 + 2 + 2) + (1 + 3) = 12.
*/


function crossingSum(matrix, a, b) {
  let rowSum = matrix[a].reduce((a,b) => a + b)
  let columnSum = 0
  for (let index = 0; index < matrix.length; index++) {
    columnSum += index !== a? matrix[index][b] : 0
  }
  return rowSum + columnSum
}

test('crossing Sum', () => {
  expect(crossingSum([[1,1,1,1], [2,2,2,2], [3,3,3,3]], 1, 3)).toEqual(12)
});

Created 2019-12-14T23:11:16+05:18 · Edit