The Wayback Machine - https://web.archive.org/web/20211005072721/https://www.geeksforgeeks.org/count-of-unique-direct-path-between-n-points-on-a-plane/
Skip to content
Related Articles

Related Articles

Improve Article

Count of Unique Direct Path Between N Points On a Plane

  • Last Updated : 05 Oct, 2021

Given N points on a plane, where each point has a direct path connecting it to a different point, the task is to count the total number of unique direct paths between the points. 

Note: The value of N will always be greater than 2.

Examples:

Input: N = 4
Output: 6
Explanation: Think of 4 points as a 4 sided polygon. There will 4 direct paths (sides of the polygon) as well as 2 diagonals (diagonals of the polygon). Hence the answer will be 6 direct paths.



Input: N = 3
Output:  3
Explanation: Think of 3 points as a 3 sided polygon. There will 3 direct paths (sides of the polygon) as well as 0 diagonals (diagonals of the polygon). Hence the answer will be 3 direct paths.

Approach: The given problem can be solved using an observation that for any N-sided there are (number of sides + number of diagonals) direct paths. For any N-sided polygon, there are N sides and N*(N – 3)/2 diagonals. Therefore, the total number of direct paths is given by N + (N * (N – 3))/2.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the total number
// of direct paths
int countDirectPath(int N)
{
    return N + (N * (N - 3)) / 2;
}
  
// Driver Code
int main()
{
  
    int N = 5;
    cout << countDirectPath(N);
  
    return 0;
}
Output:
10

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :