📌  相关文章
📜  在大小为N的环中找到从任意整数点到A和B的最小距离之和

📅  最后修改于: 2021-05-07 09:38:29             🧑  作者: Mango

给定一个标记为1N的圆环。给定两个数字AB ,您可以站在任何地方(例如X )并计算距离的总和(例如Z,即从X到A的距离+从X到B的距离)。任务是选择Z最小的方式选择X。打印由此获得的Z值。请注意X既不能等于A也不能等于B。
例子:

方法:圆上的位置AB之间有两条路径,一条为顺时针方向,另一条为逆时针方向。 Z的最佳值是选择X作为AB之间的最小路径上的任意点,然后Z等于位置之间的最小距离,除非两个位置彼此相邻,即最小距离为1 。在这种情况下,不能选择X作为它们之间的点,因为它必须与AB都不同,并且结果将为3
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum value of Z
int findMinimumZ(int n, int a, int b)
{
 
    // Change elements such that a < b
    if (a > b)
        swap(a, b);
 
    // Distance from (a to b)
    int distClock = b - a;
 
    // Distance from (1 to a) + (b to n)
    int distAntiClock = (a - 1) + (n - b + 1);
 
    // Minimum distance between a and b
    int minDist = min(distClock, distAntiClock);
 
    // If both the positions are
    // adjacent on the circle
    if (minDist == 1)
        return 3;
 
    // Return the minimum Z possible
    return minDist;
}
 
// Driver code
int main()
{
    int n = 4, a = 1, b = 2;
    cout << findMinimumZ(n, a, b);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function to return the minimum value of Z
    static int findMinimumZ(int n, int a, int b)
    {
 
        // Change elements such that a < b
        if (a > b)
        {
            swap(a, b);
        }
 
        // Distance from (a to b)
        int distClock = b - a;
 
        // Distance from (1 to a) + (b to n)
        int distAntiClock = (a - 1) + (n - b + 1);
 
        // Minimum distance between a and b
        int minDist = Math.min(distClock, distAntiClock);
 
        // If both the positions are
        // adjacent on the circle
        if (minDist == 1)
        {
            return 3;
        }
 
        // Return the minimum Z possible
        return minDist;
    }
 
    private static void swap(int x, int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4, a = 1, b = 2;
        System.out.println(findMinimumZ(n, a, b));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 implementation of the approach
# Function to return the minimum value of Z
def findMinimumZ(n, a, b):
     
    # Change elements such that a < b
    if (a > b):
        temp = a
        a = b
        b = temp
 
    # Distance from (a to b)
    distClock = b - a
 
    # Distance from (1 to a) + (b to n)
    distAntiClock = (a - 1) + (n - b + 1)
 
    # Minimum distance between a and b
    minDist = min(distClock, distAntiClock)
 
    # If both the positions are
    # adjacent on the circle
    if (minDist == 1):
        return 3
 
    # Return the minimum Z possible
    return minDist
 
# Driver code
if __name__ == '__main__':
    n = 4
    a = 1
    b = 2
    print(findMinimumZ(n, a, b))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the minimum value of Z
    static int findMinimumZ(int n, int a, int b)
    {
 
        // Change elements such that a < b
        if (a > b)
        {
            swap(a, b);
        }
 
        // Distance from (a to b)
        int distClock = b - a;
 
        // Distance from (1 to a) + (b to n)
        int distAntiClock = (a - 1) + (n - b + 1);
 
        // Minimum distance between a and b
        int minDist = Math.Min(distClock, distAntiClock);
 
        // If both the positions are
        // adjacent on the circle
        if (minDist == 1)
        {
            return 3;
        }
 
        // Return the minimum Z possible
        return minDist;
    }
 
    private static void swap(int x, int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
 
    // Driver code
    static public void Main ()
    {
        int n = 4, a = 1, b = 2;
        Console.WriteLine(findMinimumZ(n, a, b));
    }
 
}
 
/* This code contributed by ajit*/


PHP
 $b)
         
        $a = $a ^ $b;
        $b = $a ^ $b;
        $a = $a ^ $b;
 
    // Distance from (a to b)
    $distClock = $b - $a;
 
    // Distance from (1 to a) + (b to n)
    $distAntiClock = ($a - 1) + ($n - $b + 1);
 
    // Minimum distance between a and b
    $minDist = min($distClock, $distAntiClock);
 
    // If both the positions are
    // adjacent on the circle
    if ($minDist == 1)
        return 3;
 
    // Return the minimum Z possible
    return $minDist;
}
 
// Driver code
 
$n = 4;
$a = 1;
$b = 2;
echo findMinimumZ($n, $a, $b);
 
 
// This code is contributed by akt_mit
?>


输出:
3

时间复杂度: O(1)

辅助空间: O(1)