OddOneOut's blog

By OddOneOut, history, 53 minutes ago, In English

I solved 2074A — A. Draw a Square in a slightly different way.

Idea

We are given four values:

l, r, d, u

For these four points to form a square, all four distances need to be equal.

The usual way is to check:

l == r && r == d && d == u

But I used a different and simple way.

First, I find the minimum and maximum among the four values.

ll mn = min({l, r, d, u});
ll mx = max({l, r, d, u});

Now notice:

  • If all four values are equal, the minimum and maximum will also be equal.
  • If the minimum and maximum are different, then at least one value is different.

So the condition becomes:

mn == mx  →  YES
mn != mx  →  NO

Code

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

#define ll long long

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t;
    cin >> t;

    while (t--)
    {
        ll l, r, d, u;
        cin >> l >> r >> d >> u;

        ll mn = min({l, r, d, u});
        ll mx = max({l, r, d, u});

        if (mn == mx)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}

Example 1

Suppose: text 2 2 2 2

Then: text mn = 2 mx = 2 So mn == mx, therefore the answer is:

YES

Example 2

Suppose: text 1 2 1 1

Then: text mn = 1 mx = 2

So mn != mx, therefore the answer is: text NO

Complexity

For each test case: * Time: O(1) * Space: O(1)

This is basically the same condition as checking all four values directly, but using the minimum and maximum gives another simple way to think about the problem.

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
49 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by OddOneOut (previous revision, new revision, compare).