Open In App

Maximum Cuts in Chessboard such that it is not divided into 2 parts

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given M x N Chessboard. The task is to determine the Maximum number of cuts that we can make in the Chessboard such that the Chessboard is not divided into 2 parts.

Examples: 

Input: M = 2, N = 2

Output: Maximum cuts = 1

Explanation: We can only make 1 cut (mark in red). if we make 1 more cut then the chessboard will divide into 2 pieces.

2-268

Maximum Cuts in Chessboard such that it is not divided into 2 parts( 2*2 Chessboard)

Input: M = 2, N = 4

Output: Maximum cuts = 3

Explanation: We can makes 3 cuts (marks in red). if we make 1 more cut then the chessboard will divide into 2 pieces.

1-421

Maximum Cuts in Chessboard such that it is not divided into 2 parts( 2*4 Chessboard)

Approach:

To find the maximum number of cuts in an M x N chessboard without dividing it into two parts, it can observed that formula is (M-1) * (N-1). This approach involves making M-1 horizontal cuts and N-1 vertical cuts. Further cuts would result in dividing the chessboard into disconnected sections, so this formula provides the optimal solution for maximizing cuts while keeping the board intact.

Below is the implementation: 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// function that calculates the
// maximum no. of cuts
int numberOfCuts(int M, int N)
{
    int result = 0;
 
    result = (M - 1) * (N - 1);
 
    return result;
}
 
// Driver Code
int main()
{
    int M = 4, N = 4;
 
    // Calling function.
    int Cuts = numberOfCuts(M, N);
 
    cout << "Maximum cuts = " << Cuts;
 
    return 0;
}


Java




// Java implementation of above approach
 
class GFG {
     
// function that calculates the
// maximum no. of cuts
static int numberOfCuts(int M, int N)
{
    int result = 0;
 
    result = (M - 1) * (N - 1);
 
    return result;
}
 
// Driver Code
public static void main(String args[])
{
    int M = 4, N = 4;
 
    // Calling function.
    int Cuts = numberOfCuts(M, N);
 
    System.out.println("Maximum cuts = " + Cuts);
 
}
}


Python3




# Python3 implementation of
# above approach
 
# function that calculates the
# maximum no. of cuts
def numberOfCuts(M, N):
    result = 0
     
    result = (M - 1) * (N - 1)
     
    return result
 
# Driver code
if __name__=='__main__':
     
    M, N = 4, 4
     
    # Calling function.
    Cuts = numberOfCuts(M, N)
     
    print("Maximum cuts = ", Cuts)
 
# This code is contributed by
# Kriti_mangal


C#




//C#  implementation of above approach
using System;
 
public class GFG{
// function that calculates the
// maximum no. of cuts
static int numberOfCuts(int M, int N)
{
    int result = 0;
 
    result = (M - 1) * (N - 1);
 
    return result;
}
 
// Driver Code
     
    static public void Main (){
     
    int M = 4, N = 4;
    // Calling function.
    int Cuts = numberOfCuts(M, N);
 
    Console.WriteLine("Maximum cuts = " + Cuts);
    }
//This code is contributed by akt_mit   
}


Javascript




<script>
 
// Javascript implementation of above approach
 
// function that calculates the
// maximum no. of cuts
function numberOfCuts(M, N)
{
    var result = 0;
 
    result = (M - 1) * (N - 1);
 
    return result;
}
 
// Driver Code
var M = 4, N = 4;
 
// Calling function.
var Cuts = numberOfCuts(M, N);
document.write( "Maximum cuts = " + Cuts);
 
</script>


PHP




<?php
// php implementation of above approach
 
// function that calculates the
// maximum no. of cuts
function numberOfCuts($M, $N)
{
    $result = 0;
 
    $result = ($M - 1) * ($N - 1);
 
    return $result;
}
 
// Driver Code
$M = 4;
$N = 4;
 
// Calling function.
$Cuts = numberOfCuts($M, $N);
 
echo "Maximum cuts = ", $Cuts ;
 
// This code is contributed by ANKITRAI1
?>


Output

Maximum cuts = 9

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads