- 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

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)

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))

p = 4 q = 4

20

- Related Questions & Answers
- Program to find out the number of accepted invitations in Python
- Program to find number of squares in a chessboard in C++
- Program to find out the number of pairs of equal substrings in Python
- 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
- Program to find out the sum of the number of divisor of the divisors in Python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to find out number of distinct substrings in a given string in python
- Python Program to find out the number of sets greater than a given value
- Program to find out the minimum number of intercountry travels in a road trip in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Python Program to find out the number of rooms in which a prize can be hidden
- Program to find out the value of a given equation in Python
- Program to find out the value of a power of 2 in Python
- Program to find out the number of boxes to be put into the godown in Python

Advertisements