Find the minimum sum of distance to A and B from any integer point in a ring of size N
Last Updated :
14 Dec, 2022
Given a circular ring which has marking from 1 to N. Given two numbers A and B, you can stand at any place(say X) and count the total sum of the distance(say Z i.e., distance from X to A + distance from X to B). The task is to choose X in such a way that Z is minimized. Print the value of Z thus obtained. Note that X cannot neither be equal to A nor be equal to B.
Examples:
Input: N = 6, A = 2, B = 4
Output: 2
Choose X as 3, so that distance from X to A is 1, and distance from X to B is 1.
Input: N = 4, A = 1, B = 2
Output: 3
Choose X as 3 or 4, both of them gives distance as 3.
Approach: There are two paths between positions A and B on the circle, one in clockwise direction and another in an anti-clockwise. An optimal value for Z is to choose X as any point on the minimum path between A and B then Z will be equal to the minimum distance between the positions except for the case when both the positions are adjacent to each other i.e. the minimum distance is 1. In that case, X cannot be chosen as the point between them as it must be different from both A and B and the result will be 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumZ( int n, int a, int b)
{
if (a > b)
swap(a, b);
int distClock = b - a;
int distAntiClock = (a - 1) + (n - b + 1);
int minDist = min(distClock, distAntiClock);
if (minDist == 1)
return 3;
return minDist;
}
int main()
{
int n = 4, a = 1, b = 2;
cout << findMinimumZ(n, a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findMinimumZ( int n, int a, int b)
{
if (a > b)
{
swap(a, b);
}
int distClock = b - a;
int distAntiClock = (a - 1 ) + (n - b + 1 );
int minDist = Math.min(distClock, distAntiClock);
if (minDist == 1 )
{
return 3 ;
}
return minDist;
}
private static void swap( int x, int y)
{
int temp = x;
x = y;
y = temp;
}
public static void main(String[] args)
{
int n = 4 , a = 1 , b = 2 ;
System.out.println(findMinimumZ(n, a, b));
}
}
|
Python3
def findMinimumZ(n, a, b):
if (a > b):
temp = a
a = b
b = temp
distClock = b - a
distAntiClock = (a - 1 ) + (n - b + 1 )
minDist = min (distClock, distAntiClock)
if (minDist = = 1 ):
return 3
return minDist
if __name__ = = '__main__' :
n = 4
a = 1
b = 2
print (findMinimumZ(n, a, b))
|
C#
using System;
class GFG
{
static int findMinimumZ( int n, int a, int b)
{
if (a > b)
{
swap(a, b);
}
int distClock = b - a;
int distAntiClock = (a - 1) + (n - b + 1);
int minDist = Math.Min(distClock, distAntiClock);
if (minDist == 1)
{
return 3;
}
return minDist;
}
private static void swap( int x, int y)
{
int temp = x;
x = y;
y = temp;
}
static public void Main ()
{
int n = 4, a = 1, b = 2;
Console.WriteLine(findMinimumZ(n, a, b));
}
}
|
PHP
<?php
function findMinimumZ( $n , $a , $b )
{
if ( $a > $b )
$a = $a ^ $b ;
$b = $a ^ $b ;
$a = $a ^ $b ;
$distClock = $b - $a ;
$distAntiClock = ( $a - 1) + ( $n - $b + 1);
$minDist = min( $distClock , $distAntiClock );
if ( $minDist == 1)
return 3;
return $minDist ;
}
$n = 4;
$a = 1;
$b = 2;
echo findMinimumZ( $n , $a , $b );
?>
|
Javascript
<script>
function findMinimumZ(n,a,b)
{
if (a > b)
{
swap(a, b);
}
let distClock = b - a;
let distAntiClock = (a - 1) + (n - b + 1);
let minDist = Math.min(distClock, distAntiClock);
if (minDist == 1)
{
return 3;
}
return minDist;
}
function swap(x,y)
{
let temp = x;
x = y;
y = temp;
}
let n = 4, a = 1, b = 2;
document.write(findMinimumZ(n, a, b));
</script>
|
Time Complexity: O(1 ), since there is only a basic arithmetic operation that takes constant time.
Auxiliary Space: O(1), as no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...