Count squares of size K inscribed in a square of size N - GeeksforGeeks

16 Apr, 2021
Given two integers N and K, the task is to find the number of squares of size K that is inscribed in a square of size N.
Examples:
Input: N = 4, K = 2Output: 9Explanation:There are 9 squares of size 2 inscribed in a square of size 4.
Input: N = 5, K = 3Output: 9Explanation:There are 9 squares of size 3 inscribed in a square of size 5.
Approach: The key observation to solve the problem is that the total number of squares in a square of size N is (N * (N + 1)* (2*N + 1)) / 6. Therefore, the total number of squares of size K possible from a square of size N are:
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of the// above approach #include <iostream>using namespace std; // Function to calculate the number// of squares of size K in a square// of size Nint No_of_squares(int N, int K){    // Stores the number of squares    int no_of_squares = 0;     no_of_squares        = (N - K + 1) * (N - K + 1);     return no_of_squares;} // Driver Codeint main(){    // Size of the    // bigger square    int N = 5;     // Size of    // smaller square    int K = 3;    cout << No_of_squares(N, K);    return 0;}
// Java implementation of the// above approachimport java.util.*;class GFG{ // Function to calculate the// number of squares of size// K in a square of size Nstatic int No_of_squares(int N,                         int K){  // Stores the number  // of squares  int no_of_squares = 0;   no_of_squares = (N - K + 1) *                  (N - K + 1);   return no_of_squares;} // Driver Codepublic static void main(String[] args){  // Size of the  // bigger square  int N = 5;   // Size of  // smaller square  int K = 3;  System.out.print(No_of_squares(N, K));}} // This code is contributed by Princi Singh
# Python3 implementation of the# above approach # Function to calculate the# number of squares of size# K in a square of size Ndef No_of_squares(N, K):       # Stores the number    # of squares    no_of_squares = 0;    no_of_squares = (N - K + 1) * (N - K + 1);    return no_of_squares; # Driver Codeif __name__ == '__main__':       # Size of the    # bigger square    N = 5;     # Size of    # smaller square    K = 3;    print(No_of_squares(N, K)); # This code is contributed by 29AjayKumar
// C# implementation of the// above approachusing System; class GFG{ // Function to calculate the// number of squares of size// K in a square of size Nstatic int No_of_squares(int N, int K){         // Stores the number    // of squares    int no_of_squares = 0;         no_of_squares = (N - K + 1) *                    (N - K + 1);         return no_of_squares;} // Driver Codepublic static void Main(String[] args){         // Size of the    // bigger square    int N = 5;         // Size of    // smaller square    int K = 3;         Console.Write(No_of_squares(N, K));}} // This code is contributed by Amit Katiyar
<script> // JavaScript program for// the above approach // Function to calculate the// number of squares of size// K in a square of size Nfunction No_of_squares(N, K){  // Stores the number  // of squares  let no_of_squares = 0;    no_of_squares = (N - K + 1) *                  (N - K + 1);    return no_of_squares;} // Driver code     // Size of the  // bigger square  let N = 5;    // Size of  // smaller square  let K = 3;  document.write(No_of_squares(N, K));     // This code is contributed by splevel62.</script>
9
Time Complexity: O(1) Auxiliary Space: O(1)
princi singh
amit143katiyar
29AjayKumar
splevel62
square-rectangle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Closest Pair of Points | O(nlogn) Implementation
Program to find line passing through 2 Points
Maximum distance between two points in coordinate plane using Rotating Caliper's Method
Program to find area of a triangle
Quickhull Algorithm for Convex Hull
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Program to find GCD or HCF of two numbers