Maximum points of intersection n lines

23 Apr, 2021
You are given n straight lines. You have to find a maximum number of points of intersection with these n lines.Examples:  
Input : n = 4 
Output : 6

Input : n = 2
Output :1
 
Approach : As we have n number of line, and we have to find the maximum point of intersection using this n line. So this can be done using the combination. This problem can be thought of as a number of ways to select any two lines among n line. As every line intersects with others that are selected. So, the total number of points = nC2Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find maximum intersecting// points#include <bits/stdc++.h>using namespace std;#define ll long int  // nC2 = (n)*(n-1)/2;ll countMaxIntersect(ll n){   return (n) * (n - 1) / 2;} // Driver codeint main(){    // n is number of line    ll n = 8;    cout << countMaxIntersect(n) << endl;    return 0;}
// Java program to find maximum intersecting// points public class GFG {         // nC2 = (n)*(n-1)/2;    static long countMaxIntersect(long n)    {       return (n) * (n - 1) / 2;    }          // Driver code    public static void main(String args[])    {        // n is number of line        long n = 8;        System.out.println(countMaxIntersect(n));      }    // This code is contributed by ANKITRAI1}
# Python3 program to find maximum# intersecting points #nC2 = (n)*(n-1)/2def countMaxIntersect(n):    return int(n*(n - 1)/2) #Driver codeif __name__=='__main__':     # n is number of line    n = 8    print(countMaxIntersect(n)) # this code is contributed by# Shashank_Sharma
// C# program to find maximum intersecting// pointsusing System; class GFG{         // nC2 = (n)*(n-1)/2;    public static long countMaxIntersect(long n)    {    return (n) * (n - 1) / 2;    }          // Driver code    public static void Main()    {        // n is number of line        long n = 8;        Console.WriteLine(countMaxIntersect(n));    }}// This code is contributed by Soumik
<?PHP// PHP program to find maximum intersecting// points // nC2 = (n)*(n-1)/2;function countMaxIntersect($n){    return ($n) * ($n - 1) / 2;} // Driver code // n is number of line$n = 8;echo countMaxIntersect($n) . "\n"; // This code is contributed by ChitraNayal?>
<script> // Javascript program to find maximum intersecting// points // nC2 = (n)*(n-1)/2;function countMaxIntersect(n){   return (n) * (n - 1) / 2;} // Driver code // n is number of linevar n = 8;document.write( countMaxIntersect(n) ); </script>
28
 
Shashank_Sharma
ankthon
SoumikMondal
ukasp
noob2000
Combinatorial
Geometric
School Programming
Combinatorial
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subsets with sum equal to X
Find the K-th Permutation Sequence of first N natural numbers
Count Derangements (Permutation such that no element appears in its original position)
Print all possible strings of length k that can be formed from a set of n characters
Permutations of a given string using STL
Program for distance between two points on earth
Closest Pair of Points using Divide and Conquer algorithm
Optimum location of point to minimize total distance
Find if two rectangles overlap
Check if two given circles touch or intersect each other