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 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 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 positionint getPosition(int n, int m){Â Â Â Â if (m > (n / 2))Â Â Â Â Â Â Â Â return (m - (n / 2));Â
    return (m + (n / 2));}Â
// Driver codeint 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 positiondef getPosition(n, m):Â
    if (m > (n // 2)) :        return (m - (n // 2))Â
    return (m + (n // 2))Â
# Driver coden = 8m = 5print(getPosition(n, m))Â
# This code is contributed # by ihritik |
C#
// C# implementation of the approachusing 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
<?php// PHP implementation of the approachÂ
// Function to return the // required positionfunction getPosition($n, $m){Â
    if ($m > ($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?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the required positionfunction getPosition( n, m){Â Â Â Â if (m > (n / 2))Â Â Â Â Â Â Â Â return (m - parseInt(n / 2));Â
    return (m + parseInt(n / 2));}Â
// Driver codevar n = 8, m = 5;document.write(getPosition(n, m));Â
</script> |
Output:Â
1
Â
Time Complexity: O(1), as we are not using any loops.
Auxiliary Space: O(1), as we are not using any extra space.
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



