VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-bishops-that-can-be-placed-on-nn-chessboard/

⇱ Maximum bishops that can be placed on N*N chessboard - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum bishops that can be placed on N*N chessboard

Last Updated : 8 Mar, 2023

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:
We can place two bishop in a row.

Input: n = 5 
Output: 8

👁 Image

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)

  1. Place n bishops in first row
  2. Place n-2 bishops in last row. We only leave two corners of last row

👁 Image

Below is the implementation of the above approach: 


Output
8

Time Complexity: O(1)

Space Complexity: O(1)

Below is the implementation for bigger values of n:


Output
24691357802469135778

Time Complexity: O(n)

Space Complexity: O(n)

Comment
Article Tags: