Position of a person diametrically opposite on a circle
There are n people standing on the circumference of a circle. Given the position of a persom m, the task is to find the position of the person standing diametrically opposite to m on the circle.

Examples:
Input: n = 6, m = 2
Output: 5
Position 5 is opposite to 2 when there are 6 positions in totalInput: n = 8, m = 5
Output: 1
Approach: There are two cases:
- If m > n / 2 then answer will always be m – (n / 2).
- If m ≤ n / 2 then answer will always be m + (n / 2).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the required position int getPosition(int n, int m) { if (m > (n / 2)) return (m - (n / 2)); return (m + (n / 2)); } // Driver code int main() { int n = 8, m = 5; cout << getPosition(n, m); return 0; } |
Java
// Java implementation of the approach class Sol { // Function to return the required position static int getPosition(int n, int m) { if (m > (n / 2)) return (m - (n / 2)); return (m + (n / 2)); } // Driver code public static void main(String args[]) { int n = 8, m = 5; System.out.println(getPosition(n, m)); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of the approach
# Function to return the
# required position
def getPosition(n, m):
if (m > (n // 2)) :
return (m – (n // 2))
return (m + (n // 2))
# Driver code
n = 8
m = 5
print(getPosition(n, m))
# This code is contributed
# by ihritik
C#
// C# implementation of the approach using System; class GFG { // Function to return the required position static int getPosition(int n, int m) { if (m > (n / 2)) return (m - (n / 2)); return (m + (n / 2)); } // Driver code static public void Main () { int n = 8, m = 5; Console.WriteLine(getPosition(n, m)); } } // This code is contributed by ajit. |
PHP
($n / 2))
return ($m – ($n / 2));
return ($m + ($n / 2));
}
// Driver code
$n = 8;
$m = 5;
echo getPosition($n, $m);
// This code is contributed
// by ihritik
?>
1
Time Complexity: O(1)
Recommended Posts:
- Determine the position of the third person on regular N sided polygon
- Lucky alive person in a circle | Code Solution to sword puzzle
- Distributing M items in a circle of size N starting from K-th position
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Convert characters of a string to opposite case
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Time until distance gets equal to X between two objects moving in opposite direction
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
- Number of different positions where a person can stand
- Number of handshakes such that a person shakes hands only once
- Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
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.



