Given two arrays **X** and **L** of same size **N**. **X _{i}** represents the position in an infinite line.

**L**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.

_{i}**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 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 line ` `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 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 code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum number of ` `// elements without overlapping in a line ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find maximum number of ` `// elements without overlapping in a line ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## 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 line ` `def` `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 code ` `x ` `=` `[` `1` `, ` `3` `, ` `4` `, ` `5` `, ` `8` `] ` `l ` `=` `[` `10` `, ` `1` `, ` `2` `, ` `2` `, ` `5` `] ` ` ` `n ` `=` `len` `(x) ` ` ` `# Function call ` `print` `(Segment(x, l, n)) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum number of ` `// elements without overlapping in a line ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find maximum number of ` `// elements without overlapping in a line ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum number of times str1 appears as a non-overlapping substring in str2
- Maximum number of overlapping Intervals
- Maximum sum two non-overlapping subarrays of given size
- Maximum sum of lengths of non-overlapping subarrays with k as the max element.
- K maximum sums of non-overlapping contiguous sub-arrays
- K maximum sums of overlapping contiguous sub-arrays
- Maximum sum of non-overlapping subarrays of length atmost K
- Count maximum non-overlapping subarrays with given sum
- Find least non-overlapping number from a given set of intervals
- Merge Overlapping Intervals
- Non-overlapping sum of two sets
- Find the overlapping sum of two arrays
- Max sum of M non-overlapping subarrays of size K
- Find two non-overlapping pairs having equal sum in an Array
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Minimum removals required to make ranges non-overlapping
- Find Non-overlapping intervals among a given set of intervals
- Make the intervals non-overlapping by assigning them to two different processors
- Maximize count of non-overlapping subarrays with sum K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.