Find if there exists multiple ways to draw line through (x, y) to cut rectangle in equal halfs
Last Updated :
01 Mar, 2022
Given four integers W, H, x, and y. The task is to find if there exist multiple ways to draw a line through (x, y) to cut the rectangle into two equal parts. If there are multiple ways print Yes otherwise print No. The co-ordinates of rectangle are (0, 0), (W, 0), (W, H) and (0, H).
Note: The point (x, y) always lies inside or above the rectangle.
Examples:
Input: W = 2, H = 2, x = 1, y = 1
Output: Yes
Line segment joining the points (0, 0) and (2, 2)
or (0, 2) and (2, 0) etc. are all valid ways.
Input: W = 1, H = 3, x = 1, y = 2
Output: No
Approach: The rectangle can be divided into two equal parts when the line is drawn vertically, horizontally, or diagonally through the center of the rectangle. So, the answer will be Yes only if the point (x, y) is the center of the rectangle i.e. (W / 2, H / 2) otherwise the answer will be No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPossible( int w, int h, int x, int y)
{
if (x * 2 == w && y * 2 == h)
return true ;
return false ;
}
int main()
{
int w = 1, h = 2, x = 1, y = 2;
if (isPossible(w, h, x, y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isPossible( int w, int h, int x, int y)
{
if (x * 2 == w && y * 2 == h)
return true ;
return false ;
}
public static void main(String[] args)
{
int w = 1 , h = 2 , x = 1 , y = 2 ;
if (isPossible(w, h, x, y))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python 3
def isPossible(w, h, x, y):
if (x * 2 = = w and y * 2 = = h):
return True
return False
if __name__ = = '__main__' :
w = 1
h = 2
x = 1
y = 2
if (isPossible(w, h, x, y)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPossible( int w, int h,
int x, int y)
{
if (x * 2 == w && y * 2 == h)
return true ;
return false ;
}
static public void Main ()
{
int w = 1, h = 2, x = 1, y = 2;
if (isPossible(w, h, x, y))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPossible(w, h, x, y)
{
if (x * 2 == w && y * 2 == h)
return true ;
return false ;
}
let w = 1, h = 2, x = 1, y = 2;
if (isPossible(w, h, x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...