In this HackerEarth Perpendicular Lines problem solution You are given 4 points of the form (xi, yi) in the 2D plane, find out if the lines formed by (x1, y1), (x2, y2), and (x3, y3), (x4, y4) are perpendicular to each other.


HackerEarth Perpendicular Lines problem solution


HackerEarth Perpendicular Lines problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define si(x) scanf("%d", &x)
#define sc(x) scanf("%c", &x)
#define sl(x) scanf("%lld", &x)
#define pl(x) printf("%lld\n", x)
#define pi(x) printf("%d\n", x)
#define gu getchar_unlocked
#define pu putchar_unlocked
#define setbits __builtin_popcountll
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define speed ios::sync_with_stdio(false)

ll gcd(ll a, ll b){
  if(b == 0){
    return a;
  }
  else{
    return gcd(b, a % b);
  }
}


int main(){
  int t;
  si(t);
  while(t--){
    
    ll x1, x2, x3, x4, y1, y2, y3, y4, m1num, m2num, m1den, m2den;
    sl(x1); sl(y1); sl(x2); sl(y2);
    sl(x3); sl(y3); sl(x4); sl(y4);

    if(x1 == x2 && y1 == y2){          
      printf("INVALID\n");
    }
    else if(x3 == x4 && y3 == y4){
      printf("INVALID\n");
    }
    else{
      m1num = y2 - y1; m1den = x2 - x1;
      m2num = y4 - y3; m2den = x4 - x3;

      if(m1den == 0 && m2den == 0){
        printf("NO\n");
      }
      else if(m1den == 0){ 
        if(m2num == 0){
          printf("YES\n");
        }
        else{
          printf("NO\n");
        }
      }
      else if(m2den == 0){
        if(m1num == 0){
          printf("YES\n");
          
        }
        else{
          printf("NO\n");
        }
      }
      else{          
        int neg = 0;
        if(m1num < 0){
          neg++;
        }
        if(m2num < 0){
          neg++;
        }
        if(m1den < 0){
          neg++;
        }
        if(m2den < 0){
          neg++;
        }
      
        ll p = gcd(abs(m1num), abs(m1den));
        ll q = gcd(abs(m2num), abs(m2den));
        m1num /= p; m1den /= p;
        m2num /= q; m2den /= q;
      
        if(abs(m1num) == abs(m2den) && abs(m2num) == abs(m1den) && neg % 2 == 1){
          printf("YES\n");
        }
        else{
          printf("NO\n");
        }
      }
    
    }
  }
}