# Maximum number of line intersections formed through intersection of N planes

Given N planes. The task is to find the maximum number of line intersections that can be formed through the intersections of N planes.

Examples:

Input: N = 3
Output: 3

Input: N = 5
Output: 10

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

Let there be N planes such that no 3 planes intersect in a single line of intersection and no 2 planes are parallel to each other. Adding ‘N+1’th plane to this space should be possible while retaining the above two conditions. In that case, this plane would intersect each of the N planes in ‘N’ distinct lines.

Thus, the ‘N+1’th plane could atmost add ‘N’ new lines to the total count of lines of intersection. Similarly, the Nth plane could add at most “N-1′ distinct lines of intersection. It is easy therefore to see that, for ‘N’ planes, the maximum number of lines of intersection could be:

`(N-1) + (N-2) +...+ 1 = N*(N-1)/2`

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above pproach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count maximum number of  ` `// intersections possible ` `int` `countIntersections(``int` `n) ` `{ ` `    ``return` `n * (n - 1) / 2; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 3; ` ` `  `    ``cout << countIntersections(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG  ` `{ ` `     `  `    ``// Function to count maximum number of  ` `    ``// intersections possible  ` `    ``static` `int` `countIntersections(``int` `n)  ` `    ``{  ` `        ``return` `n * (n - ``1``) / ``2``;  ` `    ``}  ` `     `  `    ``// Driver Code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `n = ``3``;  ` `     `  `        ``System.out.println(countIntersections(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the above pproach ` ` `  `# Function to count maximum number of ` `# intersections possible ` `def` `countIntersections(n): ` `    ``return` `n ``*` `(n ``-` `1``) ``/``/` `2` ` `  `# Driver Code ` `n ``=` `3` ` `  `print``(countIntersections(n)) ` ` `  `# This code is contributed by mohit kumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to count maximum number of  ` `    ``// intersections possible  ` `    ``static` `int` `countIntersections(``int` `n)  ` `    ``{  ` `        ``return` `n * (n - 1) / 2;  ` `    ``}  ` `     `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{  ` `        ``int` `n = 3;  ` `     `  `        ``Console.WriteLine(countIntersections(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```3
```

Time Complexity: O(1)

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.