Programming & Development / April 19, 2025

Java Code to Find Maximum Overlapping Phone Calls

Java overlapping intervals maximum intersecting intervals phone call overlap interval scheduling Java algorithm event-based approach

1. Introduction

When managing call logs or scheduling events, a common challenge is determining the maximum number of overlapping intervals. In this tutorial, we’ll walk through a Java solution to find the maximum number of phone calls that are active (i.e., overlapping) at the same time.

2. Problem Statement

Given a list of phone calls represented as start and end times (in minutes), find the maximum number of calls that are active simultaneously.

✅ Example Input:

java

int[][] calls = {
    {1, 2},
    {2, 5},
    {3, 6},
    {5, 8},
    {4, 7}
};

✅ Output:

typescript

Maximum number of intersecting calls: 3

3. Solution Strategy

We use an event-based approach:

  • Each call generates two events: a start (+1) and an end (-1).
  • We sort all events based on time:
  • If two events have the same time, the end comes before the start to avoid overcounting.
  • As we process each event, we maintain a running count of active calls.

4. Java Code Implementation

java

import java.util.*;

public class MaximumIntersectingCalls {

    public static void main(String[] args) {
        int[][] calls = {
            {1, 2},
            {2, 5},
            {3, 6},
            {5, 8},
            {4, 7}
        };

        System.out.println("Maximum number of intersecting calls: " + findMaxIntersectingCalls(calls));
    }

    public static int findMaxIntersectingCalls(int[][] calls) {
        List<int[]> events = new ArrayList<>();

        // Step 1: Create start and end events
        for (int[] call : calls) {
            events.add(new int[]{call[0], 1});   // Start of call
            events.add(new int[]{call[1], -1});  // End of call
        }

        // Step 2: Sort events by time, prioritizing end before start on tie
        events.sort((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);

        int maxIntersecting = 0;
        int currentIntersecting = 0;

        // Step 3: Traverse events to calculate overlaps
        for (int[] event : events) {
            currentIntersecting += event[1];
            maxIntersecting = Math.max(maxIntersecting, currentIntersecting);
        }

        return maxIntersecting;
    }
}

5. Output Explanation

For the input:

{1, 2}, {2, 5}, {3, 6}, {5, 8}, {4, 7}

There is a point in time (e.g., between minutes 4 and 5) when 3 phone calls are active concurrently.

6. Time and Space Complexity

ComplexityValueTimeO(n log n) — due to sorting eventsSpaceO(n) — for storing events

7. Real-World Applications

  • 📞 Call Center Load Analysis
  • 📅 Meeting Room Scheduling
  • 🧠 Concurrency Analysis in Multithreaded Systems
  • 🚀 Event-based Monitoring in Real-Time Systems

8. Summary

This elegant Java solution uses sorting and a simple counter to efficiently compute the maximum number of overlapping intervals. It’s a go-to technique for many real-world scheduling and concurrency problems.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex