- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cell Count After Removing Corner Diagonals in Python
Suppose we have a number n representing the length of an n x n board. We have to delete all cells that are diagonal to one of the four corners and return the number of empty cells.
So, if the input is like n = 4,
X | O | O | X |
O | X | X | O |
O | X | X | O |
X | O | O | X |
Then the output will be 8.
To solve this, we will follow this formula −
- n*n - 2 * n +(n mod 2)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): return n*n - 2 * n + (n%2) ob = Solution() print(ob.solve(4))
Input
4
Output
8
Advertisements