Sunday, October 6, 2024
Google search engine
HomeData Modelling & AICount squares with odd side length in Chessboard

Count squares with odd side length in Chessboard

Given a N * N chessboard, the task is to count the number of squares having the odd side length.
Example: 
 

Input: N = 3 
Output: 10 
9 squares are possible whose sides are 1 
and a single square with side = 3 
9 + 1 = 10
Input: N = 8 
Output: 120 
 

 

Approach: For all odd numbers from 1 to N and then calculate the number of squares that can be formed having that odd side. For the ith side, the count of squares is equal to (N – i + 1)2. Further, add all such counts of squares.
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 count
// of odd length squares possible
int count_square(int n)
{
 
    // To store the required count
    int count = 0;
 
    // For all odd values of i
    for (int i = 1; i <= n; i = i + 2) {
 
        // Add the count of possible
        // squares of length i
        int k = n - i + 1;
        count += (k * k);
    }
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    int N = 8;
 
    cout << count_square(N);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the count
    // of odd length squares possible
    static int count_square(int n)
    {
 
        // To store the required count
        int count = 0;
 
        // For all odd values of i
        for (int i = 1; i <= n; i = i + 2) {
 
            // Add the count of possible
            // squares of length i
            int k = n - i + 1;
            count += (k * k);
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 8;
 
        System.out.println(count_square(N));
    }
}
 
// This code is contributed by Rajput-Ji


Python3




# Python implementation of the approach
 
# Function to return the count
# of odd length squares possible
def count_square(n):
 
    # To store the required count
    count = 0;
 
    # For all odd values of i
    for i in range(1, n + 1, 2):
 
        # Add the count of possible
        # squares of length i
        k = n - i + 1;
        count += (k * k);
 
    # Return the required count
    return count;
 
# Driver code
N = 8;
print(count_square(N));
 
# This code has been contributed by 29AjayKumar


C#




// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the count
    // of odd length squares possible
    static int count_square(int n)
    {
 
        // To store the required count
        int count = 0;
 
        // For all odd values of i
        for (int i = 1; i <= n; i = i + 2) {
 
            // Add the count of possible
            // squares of length i
            int k = n - i + 1;
            count += (k * k);
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 8;
 
        Console.WriteLine(count_square(N));
    }
}
 
// This code is contributed by Code_Mech.


PHP




<?php
// PHP implementation of the approach
 
// Function to return the count
// of odd length squares possible
function count_square($n)
{
 
    // To store the required count
    $count = 0;
 
    // For all odd values of i
    for ($i = 1; $i <= $n; $i = $i + 2)
    {
 
        // Add the count of possible
        // squares of length i
        $k =$n - $i + 1;
        $count += ($k * $k);
    }
 
    // Return the required count
    return $count;
}
 
// Driver code
$N = 8;
 
echo count_square($N);
 
// This code is contributed by AnkitRai01
?>


Javascript




<Script>
 
// Javascript implementation of the approach
 
// Function to return the count
// of odd length squares possible
function count_square(n)
{
 
    // To store the required count
    let count = 0;
 
    // For all odd values of i
    for (let i = 1; i <= n; i = i + 2) {
 
        // Add the count of possible
        // squares of length i
        let k = n - i + 1;
        count += (k * k);
    }
 
    // Return the required count
    return count;
}
 
// Driver code
    let N = 8;
 
    document.write(count_square(N));
 
</script>


Output: 

120

 

Time Complexity: O(N)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments