- Related Questions & Answers
- Program to find number of squares in a chessboard in C++
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to Find Out the Strings of the Same Size in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Magic Squares In Grid in C++
- Count Magic squares in a grid in C++
- Program to Find Out the Special Nodes in a Tree in Python
- Program to Find Out the Points Achievable in a Contest in Python
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- Create a procedure to find out the factorial of a number?
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- Program to Find Out Currency Arbitrage in Python
- Program to find out the efficient way to study in Python
- Write a C program to find out the largest and smallest number in a series

- 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

Advertisements