

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to Find Out the Number of Squares in a Grid in Python
Suppose we have two values p and q, we have to find the number of unique squares that can be generated from a grid with p rows and q columns in which the points are placed evenly. If the answer is very large return result mod 10^9 + 7. In this problem, a square is a set of 4 points that form the four vertices of a square. The sides of the square must have the same length, and it does not always have the need to be aligned with the axes of the grid.
So, if the input is like p = 4, q = 4, then the output will be 20.
To solve this, we will follow these steps −
for i in range minimum of r to c, do,
ans := ans +(r - i) *(c - i) * i
return ans mod (10^9 + 7)
Example
Let us see the following implementation to get better understanding −
class Solution: def solve(self, r, c): ans = 0 for i in range(min(r, c)): ans += (r - i) * (c - i) * i return ans % (10 ** 9 + 7) ob = Solution() print(ob.solve(4,4))
Input
p = 4 q = 4
Output
20
- Related Questions & Answers
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of border cells in a grid
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- C++ Program to find out the number of cells to block in a grid to create a path
- C++ Program to find out the number of sides that a polygon has inside a grid
- Program to find number of squares in a chessboard in C++
- C++ program to find out the maximum number of cells a cleaning robot can clean in a grid
- C++ program to find out the number of ways a grid with boards can be colored
- Program to find out the number of accepted invitations in Python
- Program to find out the number of pairs of equal substrings in Python
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- Program to find out the number of special numbers in a given range in Python
- Python Program to find out the price of a product after a number of days
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- C++ Program to find out if there is a pattern in a grid
Advertisements