# Check if any square (with one colored cell) can be divided into two equal parts

Given a square of size **n** . There are **n ^{2}** small squares inside the square

**n**of size 1 unit each, in which any one of the square is colored. Our task is to cut the square

**n**into two equal parts. The cutting line should not have any common points with the colored cell and the resulting two parts should be equal up to rotation. Print “YES” if it’s possible to cut the square with such conditions and “NO” otherwise.

**Note :**The value of

**n**should always be an even positive number.

**Examples:**

Input :n = 4, x = 1, y = 1Output :YES // n = 4 and 1 1 is the coordinate of the colored squareInput :n = 2, x = 1, y = 1Output :NO

In the first example the painted square has a coordinate 1 x 1 . So we have to cut the bigger square into two parts so that there should not be any common point with the colored cell. The bold line shown in the above picture cuts the square into two equal parts.

Below is the step by step algorithm to solve this problem:

1 . Initialize the size of the square and the position of the painted square.

2 . Dividing a square into two equal parts will only be possible if the cutting line will pass through the center of our bigger square.

3 . Thus, if the painted square is linked anyway to the center of the bigger square then it is not possible to cut the bigger square into two equal parts.

4 . So to check, divide the size of bigger square into half and check if any dimension of a painted square is linked anyway to it.

Below is the implementation of above approach:

## C++

`// C++ program to illustrate ` `// the above problem ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check if it's possible to ` `// divide the square in two equal parts ` `void` `halfsquare(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `half = n / 2; ` ` ` ` ` `// if the painted square is linked anyway ` ` ` `// to the center of the square ` ` ` `// then it's not possible ` ` ` `if` `((half == x || half == x - 1) && ` ` ` `(half == y || half == y - 1)) ` ` ` `cout << ` `"NO"` `<< endl; ` ` ` ` ` `// else yes it's possible ` ` ` `else` ` ` `cout << ` `"YES"` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialize the size of the square ` ` ` `int` `n = 100; ` ` ` ` ` `// initialize the dimention of the painted square ` ` ` `int` `x = 51, y = 100; ` ` ` ` ` `halfsquare(n, x, y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to illustrate ` `// the above problem ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// function to check if it's possible to ` `// divide the square in two equal parts ` `static` `void` `halfsquare(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `half = n / ` `2` `; ` ` ` ` ` `// if the painted square is linked anyway ` ` ` `// to the center of the square ` ` ` `// then it's not possible ` ` ` `if` `((half == x || half == x - ` `1` `) && ` ` ` `(half == y || half == y - ` `1` `)) ` ` ` `System.out.println( ` `"NO"` `); ` ` ` ` ` `// else yes it's possible ` ` ` `else` ` ` `System.out.println( ` `"YES"` `); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `// initialize the size of the square ` ` ` `int` `n = ` `100` `; ` ` ` ` ` `// initialize the dimention of the painted square ` ` ` `int` `x = ` `51` `, y = ` `100` `; ` ` ` ` ` `halfsquare(n, x, y); ` ` ` `} ` `} ` `// This code is contributed ` `// by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to illustrate ` `# the above problem ` ` ` `# function to check if it's possible to ` `# divide the square in two equal parts ` `def` `halfsquare(n, x, y) : ` ` ` `half ` `=` `n ` `/` `/` `2` ` ` ` ` `# if the painted square is ` ` ` `# linked anyway to the center ` ` ` `# of the square then it's ` ` ` `# not possible ` ` ` `if` `((half ` `=` `=` `x ` `or` `half ` `=` `=` `x ` `-` `1` `) ` `and` ` ` `(half ` `=` `=` `y ` `or` `half ` `=` `=` `y ` `-` `1` `)) : ` ` ` `print` `(` `"NO"` `) ` ` ` ` ` `# else yes it's possible ` ` ` `else` `: ` ` ` `print` `(` `"YES"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# initialize the size of the square ` ` ` `n ` `=` `100` ` ` ` ` `# initialize the dimension ` ` ` `# of the painted square ` ` ` `x, y ` `=` `51` `, ` `100` ` ` ` ` `halfsquare(n, x, y) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to illustrate ` `// the above problem ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// function to check if it's possible to ` `// divide the square in two equal parts ` `static` `void` `halfsquare(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `half = n / 2; ` ` ` ` ` `// if the painted square is linked anyway ` ` ` `// to the center of the square ` ` ` `// then it's not possible ` ` ` `if` `((half == x || half == x - 1) && ` ` ` `(half == y || half == y - 1)) ` ` ` `Console.WriteLine( ` `"NO"` `); ` ` ` ` ` `// else yes it's possible ` ` ` `else` ` ` `Console.WriteLine( ` `"YES"` `); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `// initialize the size of the square ` ` ` `int` `n = 100; ` ` ` ` ` `// initialize the dimention of the painted square ` ` ` `int` `x = 51, y = 100; ` ` ` ` ` `halfsquare(n, x, y); ` ` ` `} ` `} ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to illustrate ` `// the above problem ` ` ` `// function to check if it's ` `// possible to divide the ` `// square in two equal parts ` `function` `halfsquare(` `$n` `, ` `$x` `, ` `$y` `) ` `{ ` ` ` `$half` `= ` `$n` `/ 2; ` ` ` ` ` `// if the painted square is ` ` ` `// linked anyway to the center ` ` ` `// of the square then it's ` ` ` `// not possible ` ` ` `if` `((` `$half` `== ` `$x` `|| ` `$half` `== ` `$x` `- 1) && ` ` ` `(` `$half` `== ` `$y` `|| ` `$half` `== ` `$y` `- 1)) ` ` ` `echo` `"NO"` `; ` ` ` ` ` `// else yes it's possible ` ` ` `else` ` ` `echo` `"YES"` `; ` `} ` ` ` `// Driver code ` ` ` `// initialize the size ` `// of the square ` `$n` `= 100; ` ` ` `// initialize the dimention ` `// of the painted square ` `$x` `= 51; ` `$y` `= 100; ` ` ` `halfsquare(` `$n` `, ` `$x` `, ` `$y` `); ` ` ` `// This code is contributed ` `// by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

YES

## Recommended Posts:

- Check if a line at 45 degree can divide the plane into two equal weight parts
- Split a string in equal parts such that all parts are palindromes
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Check if a cell can be visited more than once in a String
- Minimum cuts required to divide the Circle into equal parts
- Check if a number is perfect square without finding square root
- Querying the number of distinct colors in a subtree of a colored tree using BIT
- How to check if given four points form a square
- Check if the array has an element which is equal to sum of all the remaining elements
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Partiton N into M parts such that difference between Max and Min part is smallest
- Possible cuts of a number such that maximum parts are divisible by 3
- Program to find smallest difference of angles of two parts of a given circle
- Nth non-Square number

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.