Minimum length of square to contain at least half of the given Coordinates
Given a set of N points in the 2-D plane. The task is to find the minimum value of M such that a square centered at the origin with side 2*M contains at least floor(N/2) points inside or on it.
Examples:
Input : N = 4
Points are: {(1, 2), (-3, 4), (1, 78), (-3, -7)}
Output : 4
The square with end point (4, 4), (-4, -4), (4, -4), (-4, 4) will contain the points (1, 2) and (-3, 4).
Smallest Possible value of M such that the square has at least 2 points is 4.
Input : N = 3
Points are: {(1, 2), (-3, 4), (1, 78)}
Output : 2
Square contains the point (1, 2). {floor(3/2) = 1}
Approach:
- One major observation for any point (x, y), minimum M in which this point lies is max(abs(x), abs(y)).
- Using point 1. We can find the minimum value of M for all the points and store them in an array.
- Sort the array.
- Now, array[i] denotes minimum M such that if i points are required in the square of side 2*M. (as all the points below i have the minimum value of M less than or equal to i).
- Print the value of array[floor(n/2)].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int mod( int x)
{
if (x >= 0)
return x;
return -x;
}
void findSquare( int n)
{
int points[n][2] = { { 1, 2 }, { -3, 4 },
{ 1, 78 }, { -3, -7 } };
int a[n];
for ( int i = 0; i < n; i++) {
int x, y;
x = points[i][0];
y = points[i][1];
a[i] = max(mod(x), mod(y));
}
sort(a, a + n);
int index = floor (n / 2) - 1;
cout << "Minimum M required is: " << a[index] << endl;
}
int main()
{
int N;
N = 4;
findSquare(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int mod( int x)
{
if (x >= 0 )
return x;
return -x;
}
static void findSquare( int n)
{
int points[][] = { { 1 , 2 }, { - 3 , 4 },
{ 1 , 78 }, { - 3 , - 7 } };
int []a = new int [n];
for ( int i = 0 ; i < n; i++)
{
int x, y;
x = points[i][ 0 ];
y = points[i][ 1 ];
a[i] = Math.max(mod(x), mod(y));
}
Arrays.sort(a);
int index = ( int ) (Math.floor(n / 2 ) - 1 );
System.out.println( "Minimum M required is: " + a[index]);
}
public static void main(String[] args)
{
int N;
N = 4 ;
findSquare(N);
}
}
|
Python3
def findSquare(n):
points = [[ 1 , 2 ], [ - 3 , 4 ],
[ 1 , 78 ], [ - 3 , - 7 ]]
a = [ None ] * n
for i in range ( 0 , n):
x = points[i][ 0 ]
y = points[i][ 1 ]
a[i] = max ( abs (x), abs (y))
a.sort()
index = n / / 2 - 1
print ( "Minimum M required is:" , a[index])
if __name__ = = "__main__" :
N = 4
findSquare(N)
|
C#
using System;
class GFG
{
static int mod( int x)
{
if (x >= 0)
return x;
return -x;
}
static void findSquare( int n)
{
int [,]points = new int [4,2]{ { 1, 2 }, { -3, 4 },
{ 1, 78 }, { -3, -7 } };
int []a = new int [n];
for ( int i = 0; i < n; i++)
{
int x, y;
x = points[i,0];
y = points[i,1];
a[i] = Math.Max(mod(x), mod(y));
}
Array.Sort(a);
int index = ( int ) (n / 2 - 1);
Console.WriteLine( "Minimum M required is: " + a[index]);
}
public static void Main(String []args)
{
int N;
N = 4;
findSquare(N);
}
}
|
PHP
<?php
function mod( $x )
{
if ( $x >= 0)
return $x ;
return - $x ;
}
function findSquare( $n )
{
$points = array ( array ( 1, 2 ),
array ( -3, 4 ),
array ( 1, 78 ),
array ( -3, -7 ));
$a [ $n ] = array ();
for ( $i = 0; $i < $n ; $i ++)
{
$x ; $y ;
$x = $points [ $i ][0];
$y = $points [ $i ][1];
$a [ $i ] = max(mod( $x ), mod( $y ));
}
sort( $a );
$index = floor ( $n / 2) - 1;
echo "Minimum M required is: " ,
$a [ $index ], "\n" ;
}
$N = 4;
findSquare( $N );
?>
|
Javascript
<script>
function mod(x)
{
if (x >= 0)
return x;
return -x;
}
function findSquare(n)
{
let points = [ [ 1, 2 ], [ -3, 4 ],
[ 1, 78 ], [ -3, -7 ] ];
let a = new Array(n);
for (let i = 0; i < n; i++)
{
let x, y;
x = points[i][0];
y = points[i][1];
a[i] = Math.max(mod(x), mod(y));
}
a.sort( function (a, b){ return a - b});
let index = (Math.floor(n / 2) - 1);
document.write( "Minimum M required is: " + a[index]);
}
let N;
N = 4;
findSquare(N);
</script>
|
Output:
Minimum M required is: 4
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...