Maximum bishops that can be placed on N*N chessboard

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

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the maximum number of bishops
// that can be placed on an n * n chessboard
int numberOfBishops(int n)
{
    if (n < 1)
        return 0;
    else if (n == 1)
        return 1;
    else
        return 2 * (n - 1);
}
  
// Driver code
int main()
{
    int n = 5;
    cout << numberOfBishops(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class gfg 
{
      
// Function to return the maximum 
// number of bishops that can be 
// placed on an n * n chessboard
static int numberOfBishops(int n)
{
    if (n < 1)
        return 0;
    else if (n == 1)
        return 1;
    else
        return 2 * (n - 1);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 5;
    System.out.println(numberOfBishops(n));
}
}
  
// This code is contributed by Mukul Singh.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# approach
import math as mt
  
# Function to return the maximum number 
# of bishops that can be placed on an 
# n * n chessboard
def numberOfBishops(n):
    if (n < 1):
        return 0
    elif (n == 1):
        return 1
    else:
        return 2 * (n - 1)
  
# Driver code
n = 5
print(numberOfBishops(n))
  
# This code is contributed by 
# Mohit kumar 29

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to return the maximum number 
// of bishops that can be placed on an 
// n * n chessboard 
function numberOfBishops($n
    if ($n < 1) 
        return 0; 
    else if ($n == 1) 
        return 1; 
    else
        return 2 * ($n - 1); 
  
// Driver code 
$n = 5; 
echo numberOfBishops($n);
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

8

Below is the implementation for bigger values of n:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the difference of 
// two big numbers as string
string substract(string str1, string str2)
{
    string res = "";
    int n1 = str1.length();
    int n2 = str2.length();
  
    // To make substraction easy
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());
  
    int carry = 0;
  
    for (int i = 0; i < n2; i++) {
  
        // Substract digit by bdigit
        int subst = ((str1[i] - '0')
                     - (str2[i] - '0') - carry);
  
        if (subst < 0) {
            subst = subst + 10;
            carry = 1;
        }
        else
            carry = 0;
  
        // Change subst as character and
        // add it to result string
        res.push_back(subst + '0');
    }
  
    for (int i = n2; i < n1; i++) {
        int subst = ((str1[i] - '0') - carry);
  
        if (subst < 0) {
            subst = subst + 10;
            carry = 1;
        }
        else
            carry = 0;
  
        res.push_back(subst + '0');
    }
  
    // Reverse result to make it actual number
    reverse(res.begin(), res.end());
  
    return res;
}
  
string NumberOfBishops(string a)
{
    if (a == "1")
        return a;
    else {
  
        // Substract 1 from number
        a = substract(a, "1");
  
        // Reverse the string to make calculations easier
        reverse(a.begin(), a.end());
  
        int carry = 0;
  
        // Multiply by 2
        for (int i = 0; i < a.size(); i++) {
            int tmp = a[i] - '0';
            tmp *= 2;
            tmp += carry;
            a[i] = '0' + (tmp % 10);
            carry = tmp / 10;
        }
        if (carry > 0)
            a += ('0' + carry);
  
        // Reverse the string to get actual result
        reverse(a.begin(), a.end());
  
        // Return result
        return a;
    }
}
  
// Driver code
int main()
{
    string a = "12345678901234567890";
    cout << NumberOfBishops(a) << endl;
  
    return 0;
}

chevron_right


Output:

24691357802469135778


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, Ryuga



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.