Find the largest interval that contains exactly one of the given N integers.

Given an array arr[] of N distinct integers, the task is to find the maximum element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1 ? L ? R ? 105
Â
Input: arr[] = {5, 10, 200}Â
Output: 99990Â
All possible intervals are [1, 9], [6, 199] and [11, 100000].Â
[11, 100000] has the maximum integers i.e. 99990.
Input: arr[] = {15000, 25000, 40000, 70000, 80000}Â
Output: 44999Â
Â
Â
Approach: The idea is to fix the element we want our interval to contain. Now we are interested in how much we can extend our interval to left and right without overlapping with other elements.Â
So, we first sort our array. Then for a fixed element, we determine its ends using the previous and next elements. We should also take care of corner cases that are when we fix our first and last intervals. This way for every element i, we find the maximum length of the interval.
Below is the implementation of the above approach:Â
Â
CPP
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the maximum// size of the required intervalint maxSize(vector<int>& v, int n){    // Insert the borders for array    v.push_back(0);    v.push_back(100001);    n += 2;Â
    // Sort the elements in ascending order    sort(v.begin(), v.end());Â
    // To store the maximum size    int mx = 0;    for (int i = 1; i < n - 1; i++) {Â
        // To store the range [L, R] such that        // only v[i] lies within the range        int L = v[i - 1] + 1;        int R = v[i + 1] - 1;Â
        // Total integers in the range        int cnt = R - L + 1;        mx = max(mx, cnt);    }Â
    return mx;}Â
// Driver codeint main(){Â Â Â Â vector<int> v = { 200, 10, 5 };Â Â Â Â int n = v.size();Â
    cout << maxSize(v, n);Â
    return 0;} |
Java
// Java implementation of the approachimport static java.lang.Integer.max;import java.util.*;Â
class GFG{Â
    // Function to return the maximum     // size of the required interval     static int maxSize(Vector<Integer> v, int n)     {        // Insert the borders for array         v.add(0);        v.add(100001);        n += 2;Â
        // Sort the elements in ascending order         Collections.sort(v);Â
        // To store the maximum size         int mx = 0;        for (int i = 1; i < n - 1; i++)        {Â
            // To store the range [L, R] such that             // only v[i] lies within the range             int L = v.get(i - 1) + 1;            int R = v.get(i + 1) - 1;Â
            // Total integers in the range             int cnt = R - L + 1;            mx = max(mx, cnt);        }Â
        return mx;    }Â
    // Driver code     public static void main(String[] args)     {        Integer arr[] = {200, 10, 5};        Vector v = new Vector(Arrays.asList(arr));        int n = v.size();Â
        System.out.println(maxSize(v, n));    }}Â
// This code is contributed by Princi Singh |
Python
# Python3 implementation of the approachÂ
# Function to return the maximum# size of the required intervaldef maxSize(v, n):Â
    # Insert the borders for array    v.append(0)    v.append(100001)    n += 2Â
    # Sort the elements in ascending order    v = sorted(v)Â
    # To store the maximum size    mx = 0    for i in range(1, n - 1):Â
        # To store the range [L, R] such that        # only v[i] lies within the range        L = v[i - 1] + 1        R = v[i + 1] - 1Â
        # Total integers in the range        cnt = R - L + 1        mx = max(mx, cnt)     Â
    return mxÂ
Â
# Driver codev = [ 200, 10, 5]n = len(v)Â
print(maxSize(v, n))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing System;using System.Collections.Generic; Â
class GFG{Â
    // Function to return the maximum     // size of the required interval     static int maxSize(List<int> v, int n)     {        // Insert the borders for array         v.Add(0);        v.Add(100001);        n += 2;Â
        // Sort the elements in ascending order         v.Sort();Â
        // To store the maximum size         int mx = 0;        for (int i = 1; i < n - 1; i++)        {Â
            // To store the range [L, R] such that             // only v[i] lies within the range             int L = v[i - 1] + 1;            int R = v[i + 1] - 1;Â
            // Total integers in the range             int cnt = R - L + 1;            mx = Math.Max(mx, cnt);        }Â
        return mx;    }Â
    // Driver code     public static void Main(String[] args)     {        int []arr = {200, 10, 5};        List<int> v = new List<int>(arr);        int n = v.Count;Â
        Console.WriteLine(maxSize(v, n));    }}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the maximum// size of the required intervalfunction maxSize(v, n){    // Insert the borders for array    v.push(0);    v.push(100001);    n += 2;Â
    // Sort the elements in ascending order    v.sort((a, b) => a - b);Â
    // To store the maximum size    let mx = 0;    for (let i = 1; i < n - 1; i++) {Â
        // To store the range [L, R] such that        // only v[i] lies within the range        let L = v[i - 1] + 1;        let R = v[i + 1] - 1;Â
        // Total integers in the range        let cnt = R - L + 1;        mx = Math.max(mx, cnt);    }Â
    return mx;}Â
// Driver code    let v = [ 200, 10, 5 ];    let n = v.length;Â
    document.write(maxSize(v, n));Â
</script> |
99990
Â
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


