Given two integers N and M, the task is to find the vertex diagonally opposite to the M^{th} vertex of an N-sided polygon.
Examples:
Input: N = 6, M = 2
Output: 5
Explanation:
It can be observed from the image above that the vertex opposite to vertex 5 is 2.
Input: N = 8, M = 5
Output: 1
Explanation:
It can be observed from the image above that the vertex opposite to vertex 8 is 1.
Approach: The following two cases need to be considered to solve the given problem:
- If M > N / 2: The vertex will always be M — (N / 2).
- If M ≤ N / 2: The vertex will always be M + (N / 2).
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to return the // required vertex int getPosition( int N, int M)
{ // Case 1:
if (M > (N / 2)) {
return (M - (N / 2));
}
// Case 2:
return (M + (N / 2));
} // Driver Code int main()
{ int N = 8, M = 5;
cout << getPosition(N, M);
return 0;
} |
// Java program for // the above approach class GFG{
// Function to return the // required vertex static int getPosition( int N,
int M)
{ // Case 1:
if (M > (N / 2 ))
{
return (M - (N / 2 ));
}
// Case 2:
return (M + (N / 2 ));
} // Driver Code public static void main(String[] args)
{ int N = 8 , M = 5 ;
System.out.print(getPosition(N, M));
} } // This code is contributed by Rajput-Ji |
# Python3 program for the # above approach # Function to return the # required vertex def getPosition(N, M):
# Case 1:
if (M > (N / / 2 )):
return (M - (N / / 2 ))
# Case 2:
return (M + (N / / 2 ))
# Driver Code N = 8
M = 5
print (getPosition(N, M))
# This code is contributed by code_hunt |
// C# program for the above approach using System;
class GFG{
// Function to return the // required vertex static int getPosition( int N, int M)
{ // Case 1:
if (M > (N / 2))
{
return (M - (N / 2));
}
// Case 2:
return (M + (N / 2));
} // Driver Code public static void Main(String[] args)
{ int N = 8, M = 5;
Console.Write(getPosition(N, M));
} } // This code is contributed by Amit Katiyar |
1
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Polygon with maximum sides that can be inscribed in an N-sided regular polygon
- Check if given polygon is a convex polygon or not
- Find the side of the squares which are inclined diagonally and lined in a row
- Diagonally Dominant Matrix
- Print matrix elements diagonally in spiral form
- Print matrix elements from top-left to bottom right in diagonally upward manner
- Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Position of a person diametrically opposite on a circle
- Time until distance gets equal to X between two objects moving in opposite direction
- Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Find number of diagonals in n sided convex polygon
- Program to find the Interior and Exterior Angle of a Regular Polygon
- Program to find the Circumcircle of any regular polygon
- Program to find the Perimeter of a Regular Polygon
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
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.