Open In App

Maximum points of intersections possible among X circles and Y straight lines

Last Updated : 12 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers X and Y, the task is to find the maximum number of points of intersection possible among X circles and Y straight lines.

Example:  

Input: X = 4, Y = 4 
Output: 50 
Explanation: 
4 lines intersect each other at 6 points and 4 circles intersect each other at maximum of 12 points. 
Each line intersects 4 circles at 8 points. 
Hence, 4 lines intersect four circles at a maximum of 32 points. 
Thus, required number of intersections = 6 + 12 + 32 = 50.

Input: X = 3, Y = 4 
Output: 36 
 

Approach: 
It can be observed that there are three types of intersections:  

  1. The number of ways to choose a pair of points from X circles is \binom{X}{2}     . Each such pair intersect at most two points.
  2. The number of ways to choose a pair of points from Y lines is \binom{Y}{2}     . Each such pair intersect in at most one point.
  3. The number of ways to choose one circle and one line from X circles and Y lines is X*Y     . Each such pair intersect in at most two points. 

So, the maximum number of point of intersection can be calculated as: 
=> 2*\binom{X}{2} + \binom{Y}{2} + 2*X*Y
=> X*(X - 1) + \frac{Y*(Y - 1)}{2} + 2*X*Y
 

Thus, formula to find maximum number of point of intersection of X circles and Y straight lines is: 
\frac{Y*(Y - 1)}{2} + X*(2*Y + X - 1)

Below is the implementation of the above approach:  

C++

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
int main()
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    cout << (maxPointOfIntersection(x, y));
}
 
// This code is contributed by Ritik Bansal

                    

Java

// Java program to implement
// the above approach
import java.io.*;
public class GFG{
     
static int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    System.out.print(maxPointOfIntersection(x, y));
}
}
 
// This code is contributed by Princi Singh

                    

Python3

# Python3 program to implement
# the above approach
def maxPointOfIntersection(x, y):
    k = y * ( y - 1 ) // 2
    k = k + x * ( 2 * y + x - 1 )
    return k
 
# Number of circles
x = 3
# Number of straight lines
y = 4
 
# Function Call
print(maxPointOfIntersection(x, y))

                    

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
     
static int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    Console.Write(maxPointOfIntersection(x, y));
}
}
 
// This code is contributed by Princi Singh

                    

Javascript

<script>
 
// Javascript program to implement
// the above approach
function maxPointOfIntersection(x, y)
{
    let k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
 
// Number of circles
let x = 3;
   
// Number of straight lines
let y = 4;
 
// Function Call
document.write(maxPointOfIntersection(x, y));
 
// This code is contributed by rameshtravel07
 
</script>

                    

Output: 
36

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads