Loading Now

sumAbsMatrix

Roy has a matrix of size NxN. Rows and Columns are numbered from 0 to N-1.
jth column of ith row contains absolute difference between i and j. 
In other words, Matrix[i][j] = abs(i-j) where 0 ≤ i, j < N.

Your task is to find sum of this matrix i.e.

sum = 0
for i=0 to N-1
    for j=0 to N-1
        sum += Matrix[i][j]

Example:

  •    For n = 2, the output should be sumAbsMatrix(n) = 2. Matrix will be:
0 1
1 0
  • For n = 3, the output should be sumAbsMatrix(n) = 8. Matrix will be:
0 1 2
1 0 1
2 1 0

Input/Output:

  • [execution time limit] 0.5 seconds 

  • [input] integer n

    Size of the matrix
    1 ≤ n ≤ 1000000,

  • [output] long

Post Comment

Contact