31. Powerful Integer
β GFG solution to the Powerful Integer problem: find maximum integer appearing in at least k intervals using sweep line technique with difference array. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a 2D integer array intervals[][] of length n, where each intervals[i] = [start, end] represents a closed interval (i.e., all integers from start to end, inclusive). You are also given an integer k. An integer is called Powerful if it appears in at least k intervals. Find the maximum Powerful Integer.
Note: If no integer occurs at least k times return -1.
π Examples
Example 1
Input: n = 3, intervals[][] = [[1, 3], [4, 6], [3, 4]], k = 2
Output: 4
Explanation: Integers 3 and 4 appear in 2 intervals. The maximum is 4.Example 2
Input: n = 4, intervals[][] = [[1, 4], [12, 45], [3, 8], [10, 12]], k = 3
Output: -1
Explanation: No integer appears in at least 3 intervals.Example 3
π Constraints
$1 \le n \le 10^5$
$1 \le \text{intervals}[i][0] \le \text{intervals}[i][1] \le 10^9$
$1 \le k \le 10^5$
β
My Approach
The optimal approach uses the Sweep Line technique with Difference Array to efficiently track interval overlaps and find the maximum powerful integer:
Sweep Line + Difference Array
Create Events:
For each interval
[start, end], create two events:+1at positionstart(interval begins)-1at positionend + 1(interval ends)
Process Events in Order:
Use a map to store events and process them in sorted order of positions.
Maintain a running count of active intervals at each position.
Track Powerful Integers:
When processing
+1events, increment count and check if count β₯ k.When processing
-1events, check if previous position had count β₯ k before decrementing.Keep track of the maximum position where count β₯ k.
Handle Edge Cases:
If no position satisfies count β₯ k, return -1.
Properly handle interval boundaries using
end + 1for closing events.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the number of intervals. We create 2n events and process them in sorted order using a map, which takes O(n log n) time for sorting and O(log n) for each map operation.
Expected Auxiliary Space Complexity: O(n), as we store at most 2n events in the map for processing the sweep line algorithm.
π§βπ» Code (C++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Coordinate Compression + Difference Array
π‘ Algorithm Steps:
Extract all unique coordinates from intervals and sort them
Use coordinate compression to map coordinates to indices
Apply difference array technique on compressed coordinates
Find the rightmost position where overlap count β₯ k
π Complexity Analysis:
Time: β±οΈ O(n log n) - Sorting coordinates and binary search
Auxiliary Space: πΎ O(n) - Storage for coordinates and difference array
β
Why This Approach?
Better for sparse intervals with large coordinate ranges
Explicit coordinate handling for better debugging
Cleaner separation of compression and processing logic
π 3οΈβ£ Priority Queue Based Sweep Line
π‘ Algorithm Steps:
Create events for interval starts (+1) and ends (-1) with timestamps
Sort events by position, with end events processed before start events at same position
Use running count to track current overlap at each position
Maintain the rightmost position where overlap count β₯ k
π Complexity Analysis:
Time: β±οΈ O(n log n) - Sorting events
Auxiliary Space: πΎ O(n) - Storage for events
β
Why This Approach?
Classic sweep line algorithm pattern
Handles edge cases naturally with event ordering
Scales well with large coordinate ranges
π 4οΈβ£ Brute Force with Range Sweeping
π‘ Algorithm Steps:
Find the minimum and maximum coordinates across all intervals
For each integer position in the range, count overlapping intervals
Track the rightmost position where overlap count meets threshold k
Return the rightmost valid position found
π Complexity Analysis:
Time: β±οΈ O(n * m) - Where m is the coordinate range
Auxiliary Space: πΎ O(1) - Only constant extra space
β
Why This Approach?
Simple and intuitive logic
Good for small coordinate ranges
Easy to understand and verify correctness
Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails on large coordinate ranges due to time constraints).
π 5οΈβ£ Segment Tree Range Query
π‘ Algorithm Steps:
Use coordinate compression to map all coordinates to a smaller range
Build a segment tree on the compressed coordinates
For each interval, perform range update (+1) on the segment tree
Query each position to find the maximum position with count β₯ k
π Complexity Analysis:
Time: β±οΈ O(n log n) - Coordinate compression and segment tree operations
Auxiliary Space: πΎ O(n) - Segment tree storage
β
Why This Approach?
Powerful for complex range queries
Supports dynamic updates efficiently
Good for multiple query scenarios
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π·οΈ Sweep Line with Difference Array
π’ O(n log n)
π’ O(n)
π Simple and clean
π§ Map overhead
π Coordinate Compression
π’ O(n log n)
π‘ O(n)
π Explicit coordinate handling
πΎ More complex implementation
π Priority Queue Sweep
π’ O(n log n)
π’ O(n)
β Classic sweep line pattern
π§ Complex event handling
π Brute Force (TLE)
π΄ O(n * range)
π’ O(1)
π― Simple and intuitive
π Poor performance on large ranges
π³ Segment Tree
π’ O(n log n)
π‘ O(n)
π§ Powerful for complex queries
πΎ Complex implementation
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π General purpose solution
π₯ Sweep Line with Difference Array
β β β β β
π Large sparse coordinates
π₯ Coordinate Compression
β β β β β
π§ Small coordinate range
π₯ Brute Force (TLE)
β β β ββ
π― Interview/Learning sweep line
π Priority Queue Sweep
β β β β β
π³ Multiple complex queries
ποΈ Segment Tree
β β β β β
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated