Squares in  N*N Chessboard | Practice | GeeksforGeeks

Find total number of Squares in a N*N cheesboard.
 
Example 1:
Input:
N = 1
Output:
1
Explanation:
A 1*1 chessboard has only 1 square.
Example 2:
Input:
N = 2
Output:
5
Explanation:
A 2*2 chessboard has 5 squares.
4 1*1 squares and a 2*2 square.

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function squaresInChessBoard() which takes an Integer N as input and returns the number of squares in a N*N chessboard.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
+1
princejee20192 months ago
C++ CODE || EASY TO UNDERSTAND
class Solution { public:   long long squaresInChessBoard(long long n) {       long long sum=n*(n+1)*(2*n+1)/6;       return sum;   }};
0
abhya972 months ago
// Simple C++ solution
class Solution { public:   long long squaresInChessBoard(long long N)    {              long long squ = 0;             while(N!=0)       {           squ = squ + (N * N);           N--;       }              return squ;   }};
+2
asit1234kumar32193 months ago
One Liner code, mathematical series formula approach 
return N*(N+1)*(2*N+1)/6;
0
shubham9063 months ago
// BY recursion
 
if(N==1)       return (long)1;       return N*N + squaresInChessBoard(N-1);
+1
vravikant813 months ago
class Solution { public:   long long squaresInChessBoard(long long N) {       // code here       long long square=0,n=N+1;       while(n--)       square+=n*n;       return square;   }};
+1
sejalbkadamdhad3 months ago
// C++ Solution:-
long long squaresInChessBoard(long long N) {       // code here       long long sum=0;       long long i;       for(i=1;i<=N;i++)       {           sum=sum+i*i;       }       return sum;   }
0
dishasarna20014 months ago
Expected Time Complexity: O(1)Expected Auxiliary Space: O(1)//C++ Solution
 
class Solution { public:   long long squaresInChessBoard(long long N) {       // code here       return (N*(N+1)*(2*N+1))/6;   }}; 
0
gouravmodi104 months ago
    o(1) time & space
long long squaresInChessBoard(long long n) {
        return (n*(n+1)*(2*n+1))/6;
    }
0
arish824 months ago
long long squaresInChessBoard(long long N) {
       long long ans=N*(N+1)*(2*N+1)/6;
       return ans;
   }
0
manishdoley234 months ago
Simple C++ solution
class Solution {
  public:
    long long squaresInChessBoard(long long N) {
        // code here
        long long sum = 0;
        if(N==1) return 1;
        while(N!=0){
            sum += N*N;
            N--;
        }
        return sum;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.