Determine the position of the third person on regular N sided polygon

Given ‘N’ which represent the regular N sided polygon. Two children are standing on the vertex ‘A’ and ‘B’ of this Regular N sided polygon. The task is to determine the number of that vertex another person should stand on so that the sum of the minimum jumps required to reach A and minimum jumps required to reach B is minimized.

Note:

  1. The vertices of this regular polygon are number from 1 to N in a clockwise manner.
  2. If there are multiple answers, output the least numbered vertex.

Examples:

Input: N = 6, A = 2, B = 4 
Output: Vertex = 3
Explaination: 
The another person should stand on 3rd vertex. 
As from 3rd vertex,
1 jump is required to reach A 
and 1 jump is required to reach B. 
(See figure above)


Input: N = 4, A = 1, B = 2
Output: Vertex = 3
Explaination: 
The another person should stand on 3rd or 4th vertex. 
But, as mentioned above 
we have to print least numbered vertex
that's why the output is 3.

Approach:

  • Simply calculate jumps from each vertex except vertices A and B as on that vertices children are standing and store their sum in sum variable.
  • Finally, print that position from where the sum of jumps is minimum.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find out the
// number of that vertices
int vertices(int N, int A, int B)
{
    int position = 0;
    int minisum = INT_MAX;
    int sum = 0;
    for (int i = 1; i <= N; i++) {
  
        // Another person can't stand on
        // vertex on which 2 children stand.
        if (i == A || i == B)
            continue;
  
        // calculating minimum jumps from
        // each vertex.
        else {
  
            int x = abs(i - A);
            int y = abs(i - B);
  
            // Calculate sum of jumps.
            sum = x + y;
  
            if (sum < minisum) {
                minisum = sum;
                position = i;
            }
        }
    }
    return position;
}
  
// Driver code
int main()
{
    int N = 3, A = 1, B = 2;
  
    // Calling function
    cout << "Vertex = " << vertices(N, A, B);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
class GFG 
{
      
// Function to find out the
// number of that vertices
static int vertices(int N, int A, int B)
{
    int position = 0;
    int minisum = Integer.MAX_VALUE;
    int sum = 0;
    for (int i = 1; i <= N; i++) 
    {
  
        // Another person can't stand on
        // vertex on which 2 children stand.
        if (i == A || i == B)
            continue;
  
        // calculating minimum jumps from
        // each vertex.
        else 
        {
  
            int x = Math.abs(i - A);
            int y = Math.abs(i - B);
  
            // Calculate sum of jumps.
            sum = x + y;
  
            if (sum < minisum) 
            {
                minisum = sum;
                position = i;
            }
        }
    }
    return position;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 3, A = 1, B = 2;
  
    // Calling function
    System.out.println("Vertex = " + vertices(N, A, B));
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# Function to find out the
# number of that vertices
def vertices(N, A, B):
  
    position = 0
    miniSum = 10**9
    Sum = 0
    for i in range(1, N + 1):
  
        # Another person can't stand on
        # vertex on which 2 children stand.
        if (i == A or i == B):
            continue
  
        # calculating minimum jumps from
        # each vertex.
        else:
  
            x = abs(i - A)
            y = abs(i - B)
  
            # Calculate Sum of jumps.
            Sum = x + y
  
            if (Sum < miniSum):
                miniSum = Sum
                position = i
              
    return position
  
  
# Driver code
N = 3
A = 1
B = 2
  
# Calling function
print("Vertex = ",vertices(N, A, B))
  
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
using System.Collections.Generic;
  
class GFG 
      
// Function to find out the 
// number of that vertices 
static int vertices(int N, int A, int B) 
    int position = 0; 
    int minisum = int.MaxValue; 
    int sum = 0; 
    for (int i = 1; i <= N; i++) 
    
  
        // Another person can't stand on 
        // vertex on which 2 children stand. 
        if (i == A || i == B) 
            continue
  
        // calculating minimum jumps from 
        // each vertex. 
        else
        
  
            int x = Math.Abs(i - A); 
            int y = Math.Abs(i - B); 
  
            // Calculate sum of jumps. 
            sum = x + y; 
  
            if (sum < minisum) 
            
                minisum = sum; 
                position = i; 
            
        
    
    return position; 
  
// Driver code 
public static void Main(String[] args) 
    int N = 3, A = 1, B = 2; 
  
    // Calling function 
    Console.WriteLine("Vertex = " + vertices(N, A, B)); 
  
/* This code contributed by PrinciRaj1992 */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
  
// Function to find out the
// number of that vertices
function vertices($N, $A, $B)
{
    $position = 0;
    $minisum = PHP_INT_MAX;
    $sum = 0;
    for ($i = 1; $i <= $N; $i++) {
  
        // Another person can't stand on
        // vertex on which 2 children stand.
        if ($i == $A || $i == $B)
            continue;
  
        // calculating minimum jumps from
        // each vertex.
        else {
  
            $x = abs($i - $A);
            $y = abs($i - $B);
  
            // Calculate sum of jumps.
            $sum = $x + $y;
  
            if ($sum < $minisum) {
                $minisum = $sum;
                $position = $i;
            }
        }
    }
    return $position;
}
  
    // Driver code
    $N = 3; $A = 1; $B = 2;
  
    // Calling function
    echo "Vertex = ",vertices($N, $A,$B);
      
    // This code is contributed by Ryuga
  
?>

chevron_right


Output:

Vertex = 3


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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.