There are n people standing on the circumference of a circle. Given the position of a person 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 total
Input: 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++
#include <bits/stdc++.h>
using namespace std;
int getPosition( int n, int m)
{
if (m > (n / 2))
return (m - (n / 2));
return (m + (n / 2));
}
int main()
{
int n = 8, m = 5;
cout << getPosition(n, m);
return 0;
}
|
Java
class Sol
{
static int getPosition( int n, int m)
{
if (m > (n / 2 ))
return (m - (n / 2 ));
return (m + (n / 2 ));
}
public static void main(String args[])
{
int n = 8 , m = 5 ;
System.out.println(getPosition(n, m));
}
}
|
Python3
def getPosition(n, m):
if (m > (n / / 2 )) :
return (m - (n / / 2 ))
return (m + (n / / 2 ))
n = 8
m = 5
print (getPosition(n, m))
|
C#
using System;
class GFG
{
static int getPosition( int n, int m)
{
if (m > (n / 2))
return (m - (n / 2));
return (m + (n / 2));
}
static public void Main ()
{
int n = 8, m = 5;
Console.WriteLine(getPosition(n, m));
}
}
|
PHP
<?php
function getPosition( $n , $m )
{
if ( $m > ( $n / 2))
return ( $m - ( $n / 2));
return ( $m + ( $n / 2));
}
$n = 8;
$m = 5;
echo getPosition( $n , $m );
?>
|
Javascript
<script>
function getPosition( n, m)
{
if (m > (n / 2))
return (m - parseInt(n / 2));
return (m + parseInt(n / 2));
}
var n = 8, m = 5;
document.write(getPosition(n, m));
</script>
|
Time Complexity: O(1), as we are not using any loops.
Auxiliary Space: O(1), as we are not using any extra space.