Maximum number of elements without overlapping in a Line
Last Updated :
15 Dec, 2022
Given two arrays X and L of same size N. Xi represent the position in an infinite line. Li represents the range up to which ith element can cover on both sides. The task is to select the maximum number of elements such that no two selected elements overlap if they cover the right or the left side segment.
Note: Array X is sorted.
Examples:
Input : x[] = {10, 15, 19, 20} , L[] = {4, 1, 3, 1}
Output : 4
Suppose, first element covers left side segment [6, 10]
second element covers left side segment 14, 15]
Third element covers left side segment [16, 19]
Fourth element covers right side segment [20, 21]
Input : x[] = {1, 3, 4, 5, 8}, L[] = {10, 1, 2, 2, 5}
Output : 4
Approach:
This problem can be solved greedily. We can always make the first element cover the left segment and the last element cover the right segment. For the other elements first, try to give left segment if possible otherwise try to give the right segment. If none of them are possible then leave the element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Segment( int x[], int l[], int n)
{
if (n == 1)
return 1;
int ans = 2;
for ( int i = 1; i < n - 1; i++)
{
if (x[i] - l[i] > x[i - 1])
ans++;
else if (x[i] + l[i] < x[i + 1])
{
x[i] = x[i] + l[i];
ans++;
}
}
return ans;
}
int main()
{
int x[] = {1, 3, 4, 5, 8}, l[] = {10, 1, 2, 2, 5};
int n = sizeof (x) / sizeof (x[0]);
cout << Segment(x, l, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Segment( int x[], int l[], int n)
{
if (n == 1 )
return 1 ;
int ans = 2 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
if (x[i] - l[i] > x[i - 1 ])
ans++;
else if (x[i] + l[i] < x[i + 1 ])
{
x[i] = x[i] + l[i];
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int x[] = { 1 , 3 , 4 , 5 , 8 },
l[] = { 10 , 1 , 2 , 2 , 5 };
int n = x.length;
System.out.println(Segment(x, l, n));
}
}
|
Python3
def Segment(x, l, n):
if (n = = 1 ):
return 1
ans = 2
for i in range ( 1 , n - 1 ):
if (x[i] - l[i] > x[i - 1 ]):
ans + = 1
elif (x[i] + l[i] < x[i + 1 ]):
x[i] = x[i] + l[i]
ans + = 1
return ans
x = [ 1 , 3 , 4 , 5 , 8 ]
l = [ 10 , 1 , 2 , 2 , 5 ]
n = len (x)
print (Segment(x, l, n))
|
C#
using System;
class GFG
{
static int Segment( int []x, int []l, int n)
{
if (n == 1)
return 1;
int ans = 2;
for ( int i = 1; i < n - 1; i++)
{
if (x[i] - l[i] > x[i - 1])
ans++;
else if (x[i] + l[i] < x[i + 1])
{
x[i] = x[i] + l[i];
ans++;
}
}
return ans;
}
public static void Main(String[] args)
{
int []x = {1, 3, 4, 5, 8};
int []l = {10, 1, 2, 2, 5};
int n = x.Length;
Console.WriteLine(Segment(x, l, n));
}
}
|
Javascript
<script>
function Segment(x, l, n) {
if (n == 1)
return 1;
let ans = 2;
for (let i = 1; i < n - 1; i++) {
if (x[i] - l[i] > x[i - 1])
ans++;
else if (x[i] + l[i] < x[i + 1]) {
x[i] = x[i] + l[i];
ans++;
}
}
return ans;
}
let x = [1, 3, 4, 5, 8], l = [10, 1, 2, 2, 5];
let n = x.length;
document.write(Segment(x, l, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...