Add minimum sized interval such that all intervals merge into one
Given a 2D array arr of size N which represents an interval [start, end] inclusive where (start < end), the task is to add only one interval of minimum size (size = end – start) into the given arr, such that merging all the interval result in only one interval.
Examples:
Input: arr= {{12, 20}, {24, 100}}
Output: 4
Explanation: We can add the interval [20, 24] which is the smallest possible interval.Input: arr= {{21, 43}, {26, 29}, {46, 70}}
Output: 3
Approach: Greedy algorithm:
The idea to solve this problem is that we’ll try to add intervals greedily. Since we know there is a single interval missing, we want to track the gap when adding the intervals in a sorted manner from start to end.
We can do this by keeping the track of the last interval’s end and new interval’s begin when the previous and the current intervals are non overlapping.
Follow the steps below to implement the above idea:
- First sort the intervals based on start position.
- Keep variables “start” and “end”, which stores the starting and ending point of our required answer interval. and an extra variable “prev” for storing the last occurred interval in the iteration that is needed to merge for the next upcoming intervals.
- Check if intervals overlapped or not.
- If Intervals are not overlapped
- Keep track of “start” and “end”
- Update the “prev” with current interval arr[i].
- Update “prev” after merging the overlapping intervals
- If Intervals are not overlapped
- Return the size of the interval using the end-start.
Below is the implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; // Function return the minimum size of // interval needed to add int minInterval(vector<vector< int > >& arr) { // Sort the array on start basis sort(arr.begin(), arr.end()); int n = arr.size(), start = -1, end = -1; // If size of given interval is one or // zero then return 0. if (n == 0 || n == 1) return 0; // prev store the previous interval auto prev = arr[0]; for ( int i = 1; i < n; i++) { // If intervals are not // overlapping if (max(arr[i][0], prev[0]) > min(arr[i][1], prev[1])) { // Keep track of start and end // intervals where intervals // do not overlap if (start == -1) { start = prev[1]; end = arr[i][0]; } else { end = arr[i][0]; } // Update the previous // interval prev = arr[i]; } else { // Update the previous interval // after considering these // interval are overlapping prev = { min(arr[i][0], prev[0]), max(arr[i][1], prev[1]) }; } } // Return the size of interval. return end - start; } // Driver code int main() { vector<vector< int > > arr = { { 12, 20 }, { 24, 100 } }; // Function call int result = minInterval(arr); cout << result << endl; return 0; } |
Java
// Java code for the above approach import java.io.*; import java.util.*; class GFG { // Function return the minimum size of interval needed // to add static int minInterval( int [][] arr) { int n = arr.length; int start = - 1 ; int end = - 1 ; // If size of given interval is one or zero then // return 0. if (n == 0 || n == 1 ) { return 0 ; } // prev store the previous interval var prev = arr[ 0 ]; for ( int i = 1 ; i < n; i++) { // If intervals are not // overlapping if (Math.max(arr[i][ 0 ], prev[ 0 ]) > Math.min(arr[i][ 1 ], prev[ 1 ])) { // Keep track of start and end // intervals where intervals // do not overlap if (start == - 1 ) { start = prev[ 1 ]; end = arr[i][ 0 ]; } else { end = arr[i][ 0 ]; } // Update the previous // interval prev = arr[i]; } else { // Update the previous interval // after considering these // interval are overlapping prev = new int [] { Math.min(arr[i][ 0 ], prev[ 0 ]), Math.max(arr[i][ 1 ], prev[ 1 ]) }; } } // Return the size of interval. return end - start; } public static void main(String[] args) { int [][] arr = { { 12 , 20 }, { 24 , 100 } }; // Function call int result = minInterval(arr); System.out.print(result); } } // This code is contributed by lokesh |
Python3
# python code for the above approach: # Function return the minimum size of # interval needed to add def minInterval(arr): # Sort the array on start basis arr.sort() n = len (arr) start = - 1 end = - 1 # If size of given interval is one or # zero then return 0. if (n = = 0 or n = = 1 ): return 0 # prev store the previous interval prev = arr[ 0 ] for i in range (n): # If intervals are not # overlapping if ( max (arr[i][ 0 ], prev[ 0 ]) > min (arr[i][ 1 ], prev[ 1 ])): # Keep track of start and end # intervals where intervals # do not overlap if (start = = - 1 ): start = prev[ 1 ] end = arr[i][ 0 ] else : end = arr[i][ 0 ] # Update the previous # interval prev = arr[i] else : # Update the previous interval # after considering these # interval are overlapping prev = [ min (arr[i][ 0 ], prev[ 0 ]), max (arr[i][ 1 ], prev[ 1 ])] # Return the size of interval. return end - start # Driver code arr = [[ 12 , 20 ], [ 24 , 100 ]] # Function call result = minInterval(arr) print (result) |
C#
using System; using System.Collections.Generic; public class GFG { static int minInterval( int [, ] arr) { int n = arr.GetLength(0); int start = -1; int end = -1; // If size of given interval is one or zero then // return 0. if (n == 0 || n == 1) { return 0; } // prev store the previous interval int [] prev = new int [arr.GetLength(1)]; for ( int i = 0; i < arr.GetLength(1); i++) { prev[i] = arr[0, i]; } for ( int i = 1; i < n; i++) { // If intervals are not // overlapping if (Math.Max(arr[i, 0], prev[0]) > Math.Min(arr[i, 1], prev[1])) { // Keep track of start and end // intervals where intervals // do not overlap if (start == -1) { start = prev[1]; end = arr[i, 0]; } else { end = arr[i, 0]; } // Update the previous // interval for ( int j = 0; j < arr.GetLength(1); j++) { prev[j] = arr[i, j]; } } else { // Update the previous interval // after considering these // interval are overlapping prev = new int [] { Math.Min(arr[i, 0], prev[0]), Math.Max(arr[i, 1], prev[1]) }; } } // Return the size of interval. return end - start; } static public void Main() { int [, ] arr = { { 12, 20 }, { 24, 100 } }; // Function call int result = minInterval(arr); Console.WriteLine(result); } } // This code is contributed by ishankhandelwals. |
Javascript
<script> // JavaScript code for the above approach: // Function return the minimum size of // interval needed to add const minInterval = (arr) => { // Sort the array on start basis arr.sort(); let n = arr.length, start = -1, end = -1; // If size of given interval is one or // zero then return 0. if (n == 0 || n == 1) return 0; // prev store the previous interval let prev = arr[0]; for (let i = 1; i < n; i++) { // If intervals are not // overlapping if (Math.max(arr[i][0], prev[0]) > Math.min(arr[i][1], prev[1])) { // Keep track of start and end // intervals where intervals // do not overlap if (start == -1) { start = prev[1]; end = arr[i][0]; } else { end = arr[i][0]; } // Update the previous // interval prev = arr[i]; } else { // Update the previous interval // after considering these // interval are overlapping prev = [(Math.min(arr[i][0], prev[0]), Math.max(arr[i][1], prev[1]))]; } } // Return the size of interval. return end - start; } // Driver code let arr = [[12, 20], [24, 100]]; // Function call let result = minInterval(arr); document.write(result); // This code is contributed by rakeshsahni </script> |
PHP
<?php // Function return the minimum size of // interval needed to add function minInterval( $arr ) { // Sort the array on start basis sort( $arr ); $n = count ( $arr ); $start = -1; $end = -1; // If size of given interval is one or // zero then return 0. if ( $n == 0 || $n == 1) return 0; // prev store the previous interval $prev = $arr [0]; for ( $i = 1; $i < $n ; $i ++) { // If intervals are not // overlapping if (max( $arr [ $i ][0], $prev [0]) > min( $arr [ $i ][1], $prev [1])) { // Keep track of start and end // intervals where intervals // do not overlap if ( $start == -1) { $start = $prev [1]; $end = $arr [ $i ][0]; } else { $end = $arr [ $i ][0]; } // Update the previous // interval $prev = $arr [ $i ]; } else { // Update the previous interval // after considering these // interval are overlapping $prev = array ( min( $arr [ $i ][0], $prev [0]), max( $arr [ $i ][1], $prev [1]) ); } } // Return the size of interval. return $end - $start ; } // Driver code $arr = array ( array (12, 20), array (24, 100)); // Function call $result = minInterval( $arr ); echo $result ; // This code is contributed by Kanishka Gupta ?> |
4
Time Complexity: O(N * logN), This is due to sorting.
Auxiliary Space: O(1)
Please Login to comment...