Skip to content
Related Articles

Related Articles

Improve Article
Find the number of squares inside the given square grid
  • Last Updated : 10 Jun, 2021

Given a grid of side N * N, the task is to find the total number of squares that exist inside it. All squares selected can be of any length.
Examples: 
 

Input: N = 1 
Output:
 

Input: N = 2 
Output:
 



Input: N = 4 
Output: 30 
 

 

Approach 1: Taking a few examples, it can be observed that for a grid on size N * N, the number of squares inside it will be 12 + 22 + 32 + … + N2
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the number
// of squares inside an n*n grid
int cntSquares(int n)
{
    int squares = 0;
    for (int i = 1; i <= n; i++) {
        squares += pow(i, 2);
    }
    return squares;
}
 
// Driver code
int main()
{
    int n = 4;
 
    cout << cntSquares(4);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG {
 
    // Function to return the number
    // of squares inside an n*n grid
    static int cntSquares(int n)
    {
        int squares = 0;
        for (int i = 1; i <= n; i++) {
            squares += Math.pow(i, 2);
        }
        return squares;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
 
        System.out.print(cntSquares(4));
    }
}

Python3




# Python3 implementation of the approach
 
# Function to return the number
# of squares inside an n*n grid
def cntSquares(n) :
 
    squares = 0;
    for i in range(1, n + 1) :
        squares += i ** 2;
 
    return squares;
 
# Driver code
if __name__ == "__main__" :
 
    n = 4;
 
    print(cntSquares(4));
 
# This code is contributed by AnkitRai01

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the number
    // of squares inside an n*n grid
    static int cntSquares(int n)
    {
        int squares = 0;
        for (int i = 1; i <= n; i++)
        {
            squares += (int)Math.Pow(i, 2);
        }
        return squares;
    }
 
    // Driver code
    public static void Main(String []args)
    {
        int n = 4;
 
        Console.Write(cntSquares(n));
    }
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
    // JavaScript implementation of the approach
     
    // Function to return the number
    // of squares inside an n*n grid
    function cntSquares(n)
    {
        let squares = 0;
        for (let i = 1; i <= n; i++)
        {
            squares += Math.pow(i, 2);
        }
        return squares;
    }
     
    let n = 4;
   
      document.write(cntSquares(n));
             
</script>
Output: 
30

 

Approach 2: By the use of direct formula. 
However, the sum \displaystyle \sum_{i=1}^{n}k^{2}    has the closed form (direct formula) \displaystyle \frac{n\left(n+1\right)\left(2n+1\right)}{6}    . Hence, we can employ this to calculate the sum in \displaystyle O(1)    time.
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <iostream>
 
using namespace std;
 
int cnt_squares (int n)
{
    /* Function to return the number
     of squares inside an n*n grid */
 
    return n * (n + 1) * (2 * n + 1) / 6;
}
 
// Driver code
int main()
{
    cout << cnt_squares (4) << endl;
 
    return 0;
}

Java




// Java implementation of the approach
class GFG {
    static int cntSquares (int n) {
        /* Function to return the number
        of squares inside an n*n grid */
     
        return n * (n + 1) * (2 * n + 1) / 6;
    }
 
    // Driver code
    public static void main(String args[]) {
        System.out.println (cntSquares(4));
    }
}

Python3




# Python3 implementation of the approach
 
"""
Function to return the number
of squares inside an n*n grid
"""
 
def cntSquares(n) :
    return int (n * (n + 1) * (2 * n + 1) / 6)
 
# Driver code
if __name__ == "__main__" :
    print (cntSquares (4));

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    /* Function to return the number
     of squares inside an n*n grid */
    static int cntSquares (int n)
    {
        return n * (n + 1) * (2 * n + 1) / 6;
    }
 
    // Driver code
    public static void Main (String[] args)
    {
        Console.Write (cntSquares (4));
    }
}

Javascript




<script>
    // Javascript implementation of the approach
     
    /* Function to return the number
     of squares inside an n*n grid */
    function cntSquares (n)
    {
        return n * (n + 1) * (2 * n + 1) / 6;
    }
     
    document.write(cntSquares(4));
 
// This code is contributed by divyeshrabadiya07.
</script>
Output: 
30

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :