Doubled Distances Solution || Codechef May Cook-Off 2022

          

Doubled Distances Solution 

Codechef May Cook-Off 2022
Problem Code : DOUBLEDDIST

Solution :

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    ll t = 1;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        ll a[n];
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        sort(a, a + n);
        ll b[n - 1];
        for (ll i = 1; i < n; i++)
        {
            b[i - 1] = a[i] - a[i - 1];
        }
        bool ans = true;
        for (ll i = 1; i < n - 1; i++)
        {

            if (b[i] == (2 * b[i - 1]) || b[i] == b[i - 1] / 2)
            {
                ans = true;
            }
            else
            {
                ans = false;
                break;
            }
        }
        if (ans)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
}

Comments