![]() |
VOOZH | about |
Given an integer n, the task is to print the maximum number of bishops that can be placed on a n x n chessboard so that no two bishops attack each other. For example, maximum 2 bishops can be placed safely on 2 x 2 chessboard.
Examples:
Input: n = 2
Output: 2
We can place two bishop in a row.Input: n = 5
Output: 8
Approach: A bishop can travel in any of the four diagonals. Therefore we can place bishops if it is not in any diagonal of another bishop. The maximum bishops that can be placed on an n * n chessboard will be 2 * (n - 1).
Below is the implementation of the above approach:
8
Time Complexity: O(1)
Space Complexity: O(1)
Below is the implementation for bigger values of n:
24691357802469135778
Time Complexity: O(n)
Space Complexity: O(n)