📌  相关文章
📜  查找恰好包含给定N个整数之一的最大间隔。

📅  最后修改于: 2021-04-22 03:15:04             🧑  作者: Mango

给定N个不同整数的数组arr [] ,任务是找到区间[L,R]中的最大元素,以使区间恰好包含给定的N个整数之一,且1≤L≤R≤10 5

方法:想法是修复我们希望间隔包含的元素。现在,我们对在不与其他元素重叠的情况下可以向左和向右扩展间隔有多少感兴趣。
因此,我们首先对数组进行排序。然后,对于固定元素,我们使用上一个和下一个元素确定其末端。我们还应注意在处理第一个和最后一个间隔时遇到的极端情况。这样,对于每个元素i,我们都会找到间隔的最大长度。

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum
// size of the required interval
int maxSize(vector& 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 code
int main()
{
    vector v = { 200, 10, 5 };
    int n = v.size();
  
    cout << maxSize(v, n);
  
    return 0;
}


Java
// Java implementation of the approach
import static java.lang.Integer.max;
import java.util.*;
  
class GFG
{
  
    // Function to return the maximum 
    // size of the required interval 
    static int maxSize(Vector 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 interval
def 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 code
v = [ 200, 10, 5]
n = len(v)
  
print(maxSize(v, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GFG
{
  
    // Function to return the maximum 
    // size of the required interval 
    static int maxSize(List 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 v = new List(arr);
        int n = v.Count;
  
        Console.WriteLine(maxSize(v, n));
    }
}
  
// This code is contributed by 29AjayKumar


输出:
99990