CHESSDIST - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: Daanish Mahajan
Tester: Harris Leung
Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

The Chessboard Distance for any two points (X_1,Y_1) and (X_2,Y_2) on a Cartesian plane is defined as max(|X_1−X2|,|Y1−Y2|).

You are given two points .(X_1,Y_1) and (X_2,Y_2) Output their Chessboard Distance.

EXPLANATION:

Let’s say S_X is the absolute difference between the X coordinates while S_Y is the absolute difference between y coordinates i.e.

S_X = abs(X_1-X_2) \\ S_Y = abs(Y_1 - Y_2);

Our answer is the maximum of S_X and S_Y. Hence if S_X is greater print S_X otherwise print S_Y.

TIME COMPLEXITY:

O(1) per test case

SOLUTIONS:

Setter

Tester
Editorialist
#include<bits/stdc++.h>
using namespace std;

void solve()
{
  int a,b,c,d;
  cin>>a>>b>>c>>d;

  cout<<max(abs(a-c),abs(b-d))<<"\n";
}

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int t;
  cin>>t;

  while(t--)
    solve();

return 0;
}

def chessboard_distance(x1, y1, x2, y2):
  dx = abs(x1 - x2)  # Difference in x-coordinates
  dy = abs(y1 - y2)  # Difference in y-coordinates
  return max(dx, dy)  # Return the larger difference

# --- Test Cases --- #
T = int(input())
for _ in range(T):
  x1, y1, x2, y2 = map(int, input().split())
  distance = chessboard_distance(x1, y1, x2, y2)
  print(distance)