Minimum length of square to contain at least half of the given Coordinates | C ++ implementation of the above approach ; Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }