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.
position of a person diametrically opposite to him in 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


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
?>

Output:

1

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : andrew1234, jit_t, ihritik