Program to find length of longest interval from a list of intervals in Python

Suppose we have a list of intervals where each interval is in form [start, end]. We have to find the longest interval that we can make by merging any number of overlapping intervals.

So, if the input is like [[1, 6],[4, 9],[5, 6],[11, 14],[16, 20]], then the output will be 9, as after merging, we have the interval [1, 9] of length 9.

Algorithm Steps

To solve this, we will follow these steps ?

  • Sort the list of intervals by start time
  • Initialize union with the first interval from the intervals list
  • Initialize best with the length of the first interval
  • For each remaining interval with start time s and end time e, do:
    • If s <= union[end], then merge by updating union[end] = max(union[end], e)
    • Otherwise, start a new union with interval [s, e]
    • Update best with the maximum length found so far
  • Return best

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, intervals):
        intervals.sort()
        union = intervals[0]
        best = union[1] - union[0] + 1
        
        for s, e in intervals[1:]:
            if s <= union[1]:
                union[1] = max(union[1], e)
            else:
                union = [s, e]
            best = max(best, union[1] - union[0] + 1)
        
        return best

# Test the solution
ob = Solution()
intervals = [[1, 6], [4, 9], [5, 6], [11, 14], [16, 20]]
print("Longest interval length:", ob.solve(intervals))

The output of the above code is ?

Longest interval length: 9

How It Works

The algorithm works by:

  1. Sorting intervals: [[1, 6], [4, 9], [5, 6], [11, 14], [16, 20]]
  2. First union: [1, 6] with length 6
  3. Merging [4, 9]: Since 4 ? 6, merge to get [1, 9] with length 9
  4. Merging [5, 6]: Since 5 ? 9, union remains [1, 9]
  5. New union [11, 14]: Since 11 > 9, start new union with length 4
  6. New union [16, 20]: Since 16 > 14, start new union with length 5

The maximum length found is 9 from the merged interval [1, 9].

Alternative Approach

Here's a more straightforward implementation without using a class ?

def find_longest_interval(intervals):
    if not intervals:
        return 0
    
    intervals.sort()
    max_length = 0
    current_start, current_end = intervals[0]
    
    for start, end in intervals:
        if start <= current_end:
            # Merge intervals
            current_end = max(current_end, end)
        else:
            # Update max length and start new interval
            max_length = max(max_length, current_end - current_start + 1)
            current_start, current_end = start, end
    
    # Don't forget the last interval
    max_length = max(max_length, current_end - current_start + 1)
    return max_length

# Test the function
intervals = [[1, 6], [4, 9], [5, 6], [11, 14], [16, 20]]
result = find_longest_interval(intervals)
print("Longest interval length:", result)

The output of the above code is ?

Longest interval length: 9

Conclusion

The key insight is to sort intervals by start time, then merge overlapping intervals while tracking the maximum length. The time complexity is O(n log n) due to sorting, and space complexity is O(1).

Updated on: 2026-03-25T12:35:35+05:30

588 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements