Find the largest interval that contains exactly one of the given N integers.
Last Updated :
21 Jun, 2022
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
#include <bits/stdc++.h>
using namespace std;
int maxSize(vector< int >& v, int n)
{
v.push_back(0);
v.push_back(100001);
n += 2;
sort(v.begin(), v.end());
int mx = 0;
for ( int i = 1; i < n - 1; i++) {
int L = v[i - 1] + 1;
int R = v[i + 1] - 1;
int cnt = R - L + 1;
mx = max(mx, cnt);
}
return mx;
}
int main()
{
vector< int > v = { 200, 10, 5 };
int n = v.size();
cout << maxSize(v, n);
return 0;
}
|
Java
import static java.lang.Integer.max;
import java.util.*;
class GFG
{
static int maxSize(Vector<Integer> v, int n)
{
v.add( 0 );
v.add( 100001 );
n += 2 ;
Collections.sort(v);
int mx = 0 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
int L = v.get(i - 1 ) + 1 ;
int R = v.get(i + 1 ) - 1 ;
int cnt = R - L + 1 ;
mx = max(mx, cnt);
}
return mx;
}
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));
}
}
|
Python
def maxSize(v, n):
v.append( 0 )
v.append( 100001 )
n + = 2
v = sorted (v)
mx = 0
for i in range ( 1 , n - 1 ):
L = v[i - 1 ] + 1
R = v[i + 1 ] - 1
cnt = R - L + 1
mx = max (mx, cnt)
return mx
v = [ 200 , 10 , 5 ]
n = len (v)
print (maxSize(v, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxSize(List< int > v, int n)
{
v.Add(0);
v.Add(100001);
n += 2;
v.Sort();
int mx = 0;
for ( int i = 1; i < n - 1; i++)
{
int L = v[i - 1] + 1;
int R = v[i + 1] - 1;
int cnt = R - L + 1;
mx = Math.Max(mx, cnt);
}
return mx;
}
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));
}
}
|
Javascript
<script>
function maxSize(v, n)
{
v.push(0);
v.push(100001);
n += 2;
v.sort((a, b) => a - b);
let mx = 0;
for (let i = 1; i < n - 1; i++) {
let L = v[i - 1] + 1;
let R = v[i + 1] - 1;
let cnt = R - L + 1;
mx = Math.max(mx, cnt);
}
return mx;
}
let v = [ 200, 10, 5 ];
let n = v.length;
document.write(maxSize(v, n));
</script>
|
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...