Maximum number of segments that can contain the given points
Given an array arr[] containing N integers and two integers X and Y. Consider N line segments, where each line segment has starting and ending point as arr[i] – X and arr[i] + Y respectively.
Given another array b[] of M points. The task is to assign these points to segments such that the number of segments that have been assigned a point is maximum. Note that a point can be assigned to at most 1 segment.
Examples:
Input: arr[] = {1, 5}, b = {1, 1, 2}, X = 1, Y = 4
Output: 1
Line Segments are [1-X, 1+Y] , [5-X, 5+Y] i.e. [0, 5] and [4, 9]
The point 1 can be assigned to the first segment [0, 5]
No points can be assigned to the second segment.
So 2 can also be assigned to the first segment but it will not maximize the no. of segment.
So the answer is 1.
Input: arr[] = {1, 2, 3, 4}, b = {1, 3, 5}, X = 0, Y = 0
Output: 2
Approach: Sort both the input arrays. Now for every segment, we try to assign it the first unassigned point possible. If the current segment ends before the current point, it means that we won’t able to assign any point to it since all the points ahead of it are greater than the current point and the segment has already ended.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPoints( int n, int m, vector< int > a,
vector< int > b, int x, int y)
{
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int j = 0;
int count = 0;
for ( int i = 0; i < n; i++) {
while (j < m) {
if (a[i] + y < b[j])
break ;
if (b[j] >= a[i] - x && b[j] <= a[i] + y) {
count++;
j++;
break ;
}
else
j++;
}
}
return count;
}
int main()
{
int x = 1, y = 4;
vector< int > a = { 1, 5 };
int n = a.size();
vector< int > b = { 1, 1, 2 };
int m = a.size();
cout << countPoints(n, m, a, b, x, y);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPoints( int n, int m, int a[],
int [] b, int x, int y)
{
Arrays.sort(a);
Arrays.sort(b);
int j = 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
while (j < m)
{
if (a[i] + y < b[j])
break ;
if (b[j] >= a[i] - x && b[j] <= a[i] + y)
{
count++;
j++;
break ;
}
else
j++;
}
}
return count;
}
public static void main(String args[])
{
int x = 1 , y = 4 ;
int [] a = { 1 , 5 };
int n = a.length;
int [] b = { 1 , 1 , 2 };
int m = a.length;
System.out.println(countPoints(n, m, a, b, x, y));
}
}
|
Python3
def countPoints(n, m, a, b, x, y):
a.sort()
b.sort()
j, count = 0 , 0
for i in range ( 0 , n):
while j < m:
if a[i] + y < b[j]:
break
if (b[j] > = a[i] - x and
b[j] < = a[i] + y):
count + = 1
j + = 1
break
else :
j + = 1
return count
if __name__ = = "__main__" :
x, y = 1 , 4
a = [ 1 , 5 ]
n = len (a)
b = [ 1 , 1 , 2 ]
m = len (b)
print (countPoints(n, m, a, b, x, y))
|
C#
using System;
class GFG
{
static int countPoints( int n, int m, int []a,
int []b, int x, int y)
{
Array.Sort(a);
Array.Sort(b);
int j = 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
while (j < m)
{
if (a[i] + y < b[j])
break ;
if (b[j] >= a[i] - x && b[j] <= a[i] + y)
{
count++;
j++;
break ;
}
else
j++;
}
}
return count;
}
public static void Main()
{
int x = 1, y = 4;
int [] a = {1, 5};
int n = a.Length;
int [] b = {1, 1, 2};
int m = a.Length;
Console.WriteLine(countPoints(n, m, a, b, x, y));
}
}
|
PHP
<?php
function countPoints( $n , $m , $a , $b , $x , $y )
{
sort( $a );
sort( $b );
$j = 0;
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
while ( $j < $m )
{
if ( $a [ $i ] + $y < $b [ $j ])
break ;
if ( $b [ $j ] >= $a [ $i ] - $x &&
$b [ $j ] <= $a [ $i ] + $y )
{
$count ++;
$j ++;
break ;
}
else
$j ++;
}
}
return $count ;
}
$x = 1;
$y = 4;
$a = array ( 1, 5 );
$n = count ( $a );
$b = array ( 1, 1, 2 );
$m = count ( $b );
echo countPoints( $n , $m , $a , $b , $x , $y );
?>
|
Javascript
<script>
function countPoints(n, m, a, b, x, y)
{
a.sort( function (a, b){ return a - b});
b.sort( function (a, b){ return a - b});
let j = 0;
let count = 0;
for (let i = 0; i < n; i++)
{
while (j < m)
{
if (a[i] + y < b[j])
break ;
if (b[j] >= a[i] - x && b[j] <= a[i] + y)
{
count++;
j++;
break ;
}
else
j++;
}
}
return count;
}
let x = 1, y = 4;
let a = [1, 5];
let n = a.length;
let b = [1, 1, 2];
let m = a.length;
document.write(countPoints(n, m, a, b, x, y));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...