Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Highways problem solution

YASH PAL, 31 July 2024
In this HackerEarth Highways problem solution, Some country X is a square of N x N cells. Its inhabitants use a coordinate system in which the lower-left and upper-right corners of the square have coordinates (0,0) and (N,N). There are K cities in the country, each at a point with integer coordinates.
Residents of country X move around the country parallel to the coordinate axes. To speed up travel to neighboring countries, the government has decided to build two highways. Highways must be perpendicular to each other and parallel to the coordinate axes. Each trunk will connect two opposite sides of the square.
The distance from the city to the highways will be the distance from the city to the nearest one. The government has decided to place highways so that the maximum distance from cities to highways is the minimum possible.
Your task is to find the optimal distance from the farthest city to the nearest highway.
HackerEarth Highways problem solution

HackerEarth Highways problem solution.

#include <bits/stdc++.h>

using namespace std;

struct Town{
int x;
int y;
};

vector<Town> towns;
vector<int> MinYFromBegin, MaxYFromBegin, MinYFromEnd, MaxYFromEnd;
int n, m, x, y, length, MinDistance, MaxDistance, XAnswer, YAnswer;

bool comp( Town a, Town b ){
return ( a.x < b.x );
}

long long min( long long x, long long y ){
return ( x < y ) ? x : y;
}

long long max( long long x, long long y ){
return ( x > y ) ? x : y;
}

bool check( int distance ){
int i = 0;
int j = 0;
int MinY, MaxY;
while ( ( i < m || j < m ) ){
bool change = false;
if ( i == 0 && j == 0 ){
MinY = MinYFromEnd[i+1];
MaxY = MaxYFromEnd[i+1];
} else if ( i == 0 && j == m-1 ){
MinY = MaxY = 0;
} else if ( i == 0 && j < m-1 ){
MinY = MinYFromEnd[j+1];
MaxY = MaxYFromEnd[j+1];
} else if ( i > 0 && j == m-1 ){
MinY = MinYFromBegin[i-1];
MaxY = MaxYFromBegin[i-1];
} else {
MinY = min( MinYFromBegin[i-1], MinYFromEnd[j+1] );
MaxY = max( MaxYFromBegin[i-1], MaxYFromEnd[j+1] );
}
if ( ( MaxY-MinY <= 2*distance ) && ( towns[j].x - towns[i].x <= 2*distance ) ){
XAnswer = min( towns[i].x + distance, n );
YAnswer = min( MinY + distance, n );
return true;
}
if ( towns[j].x - towns[i].x <= 2*distance ){
if ( j < m-1 ){
j++;
change = true;
}
} else {
if ( i < m-1 ){
i++;
change = true;
}
}
if ( !change ){
return false;
}
}
}

int main(){
scanf( "%d %d", &n, &m );
for ( int i = 0; i < m; i++ ){
scanf( "%d %d", &x, &y );
Town t;
t.x = x;
t.y = y;
towns.push_back( t );
}
sort( towns.begin(), towns.end(), comp );
MinYFromBegin.push_back( towns[0].y );
MaxYFromBegin.push_back( towns[0].y );
for ( int i = 1; i < m; i++ ){
MinYFromBegin.push_back( min( MinYFromBegin[i-1], towns[i].y ) );
MaxYFromBegin.push_back( max( MaxYFromBegin[i-1], towns[i].y ) );
}
MinYFromEnd.resize( m );
MaxYFromEnd.resize( m );
MinYFromEnd[m-1] = towns[m-1].y;
MaxYFromEnd[m-1] = towns[m-1].y;
for ( int i = m-2; i >= 0; i-- ){
MinYFromEnd[i] = min( MinYFromEnd[i+1], towns[i].y );
MaxYFromEnd[i] = max( MaxYFromEnd[i+1], towns[i].y );
}
MinDistance = -1;
MaxDistance = n;
while ( MaxDistance - MinDistance > 1 ){
length = ( MaxDistance + MinDistance ) / 2;
if ( check( length ) ){
MaxDistance = length;
} else {
MinDistance = length;
}
}
printf( "%dn", MaxDistance);
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes