Maximum number of elements without overlapping in a Line

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++
// CPP program to find maximum number of // elements without overlapping in a line#include <bits/stdc++.h>using namespace std;Â
// Function to find maximum number of // elements without overlapping in a lineint Segment(int x[], int l[], int n){    // If n = 1, then answer is one    if (n == 1)        return 1;         // We can always make 1st element to cover     // left segment and nth the right segment    int ans = 2;                      for (int i = 1; i < n - 1; i++)    {        // If left segment for ith element doesnt overlap        // with i - 1 th element then do left        if (x[i] - l[i] > x[i - 1])            ans++;Â
        // else try towards right if possible        else if (x[i] + l[i] < x[i + 1])        {            // update x[i] to right endpoint of             // segment covered by it            x[i] = x[i] + l[i];            ans++;        }    }         // Return the required answer    return ans;}Â
// Driver codeint main(){Â Â Â Â int x[] = {1, 3, 4, 5, 8}, l[] = {10, 1, 2, 2, 5};Â Â Â Â Â Â Â Â Â int n = sizeof(x) / sizeof(x[0]);Â
    // Function call    cout << Segment(x, l, n);Â
    return 0;} |
Java
// Java program to find maximum number of // elements without overlapping in a lineimport java.util.*;Â
class GFG{Â
// Function to find maximum number of // elements without overlapping in a linestatic int Segment(int x[], int l[], int n){    // If n = 1, then answer is one    if (n == 1)        return 1;         // We can always make 1st element to cover     // left segment and nth the right segment    int ans = 2;             for (int i = 1; i < n - 1; i++)    {        // If left segment for ith element         // doesn't overlap with i - 1 th        // element then do left        if (x[i] - l[i] > x[i - 1])            ans++;Â
        // else try towards right if possible        else if (x[i] + l[i] < x[i + 1])        {            // update x[i] to right endpoint of             // segment covered by it            x[i] = x[i] + l[i];            ans++;        }    }         // Return the required answer    return ans;}Â
// Driver codepublic static void main(String[] args) {    int x[] = {1, 3, 4, 5, 8},        l[] = {10, 1, 2, 2, 5};         int n = x.length;Â
    // Function call    System.out.println(Segment(x, l, n));}}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 program to find maximum number of# elements without overlapping in a lineÂ
# Function to find maximum number of# elements without overlapping in a linedef Segment(x, l, n):         # If n = 1, then answer is one    if (n == 1):        return 1Â
    # We can always make 1st element to cover    # left segment and nth the right segment    ans = 2         for i in range(1, n - 1):                 # If left segment for ith element doesnt overlap        # with i - 1 th element then do left        if (x[i] - l[i] > x[i - 1]):            ans += 1Â
        # else try towards right if possible        elif (x[i] + l[i] < x[i + 1]):                         # update x[i] to right endpoof            # segment covered by it            x[i] = x[i] + l[i]            ans += 1Â
    # Return the required answer    return ansÂ
# Driver codex = [1, 3, 4, 5, 8]l = [10, 1, 2, 2, 5]Â
n = len(x)Â
# Function callprint(Segment(x, l, n))Â
# This code is contributed # by Mohit Kumar |
C#
// C# program to find maximum number of // elements without overlapping in a lineusing System;Â Â Â Â Â class GFG{Â
// Function to find maximum number of // elements without overlapping in a linestatic int Segment(int []x, int []l, int n){    // If n = 1, then answer is one    if (n == 1)        return 1;         // We can always make 1st element to cover     // left segment and nth the right segment    int ans = 2;             for (int i = 1; i < n - 1; i++)    {        // If left segment for ith element         // doesn't overlap with i - 1 th        // element then do left        if (x[i] - l[i] > x[i - 1])            ans++;Â
        // else try towards right if possible        else if (x[i] + l[i] < x[i + 1])        {            // update x[i] to right endpoint of             // segment covered by it            x[i] = x[i] + l[i];            ans++;        }    }         // Return the required answer    return ans;}Â
// Driver codepublic static void Main(String[] args) {Â Â Â Â int []x = {1, 3, 4, 5, 8};Â Â Â Â int []l = {10, 1, 2, 2, 5};Â Â Â Â Â Â Â Â Â int n = x.Length;Â
    // Function call    Console.WriteLine(Segment(x, l, n));}}Â
// This code is contributed by PrinciRaj1992 |
Javascript
<script>Â
// JavaScript program to find maximum number of// elements without overlapping in a lineÂ
// Function to find maximum number of// elements without overlapping in a linefunction Segment(x, l, n) {    // If n = 1, then answer is one    if (n == 1)        return 1;Â
    // We can always make 1st element to cover    // left segment and nth the right segment    let ans = 2;Â
Â
    for (let i = 1; i < n - 1; i++) {        // If left segment for ith element doesnt overlap        // with i - 1 th element then do left        if (x[i] - l[i] > x[i - 1])            ans++;Â
        // else try towards right if possible        else if (x[i] + l[i] < x[i + 1]) {            // update x[i] to right endpolet of            // segment covered by it            x[i] = x[i] + l[i];            ans++;        }    }Â
    // Return the required answer    return ans;}Â
// Driver codeÂ
let x = [1, 3, 4, 5, 8], l = [10, 1, 2, 2, 5];Â
let n = x.length;Â
// Function calldocument.write(Segment(x, l, n));Â
// This code is contributed by _saurabh_jaiswalÂ
</script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



