Roy and Matrix Sum
Practice
4.2 (6 votes)
Implementation
Basic programming
Ad Hoc
Easy
Mathematics
Open
Approved
Mathamatics
Problem
84% Success 9859 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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]

Caution: Use 64-bit integer for sum to avoid overflow

Input:
First line contains T - number of test cases
Following T lines each contains an integer N - size of matrix

Output:
For each test case output the result in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 1000000

Sample Test Explanation:

Test Case 1:
Matrix will be:

0 1
1 0

Hence the sum is 2.

Test Case 2:
Matrix will look like:

0 1 2
1 0 1
2 1 0

The sum of above matrix is 8.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
10 votes
Tags:
ImplementationMath BasicBasics of ImplementationC++Basic MathMathGame Theory
Points:20
10 votes
Tags:
OpenAlgorithmsApprovedEasy
Points:20
15 votes
Tags:
Simple-mathEasyGreedy algorithmMathematicsMathematicsMathamatics