-1

This is the problem : https://codeforces.com/contest/567/problem/A

Summary of Problem: Basically there are different cities on a number line and we must calculate the maximum and minimum cost of sending a mail from one city to any other city on the list. The cost of sending mail is exactly equal to the distance between two given cities.

This is my code:

#include <iostream>
#define ll long long

using namespace std;

int main () {
    ll n;
    int a[n];
    int b[n];
    ll maxx;
    ll minn;
    for (ll i=0; i<n; i++) {
        cin >> a[i];
    }
    ll i=0;
    for (ll i=0; i<n; i++) {
        ll j=0;
        for (ll j=0; j<n; j++) {
            ll b[i]={a[j]-a[i]};
        }
        sort (b, b+n);
        ll minn= b[1];
        ll maxx= b[n];
        cout << minn << maxx;
    }

}

I am open to learning anything new that would be useful for me, feel free to make suggestions since I am still a beginner.

  • `ll n; int a[n]; int b[n];` -- This is not valid C++. Arrays in C++ must have their size denoted by a compile-time expression, not a runtime value. Dynamic arrays in C++ are achieved by utilizing `std::vector`, i.e. `std::vector a(n), b(n);`. Also, that `ll` macro is totally unnecessary and just obfuscates the code. Simply using `int64_t` is far more preferable than a macro that looks like the number `11`. – PaulMcKenzie May 21 '22 at 11:28
  • `sort (b, b+n);` -- Then the behavior of this is implementation defined, since `b` is not an actual array, but something the compiler vendor concocted. Using vector, this would be `std::sort(b.begin(), b.end());`. As a matter of fact, there were some earlier versions of compilers that did allow the non-standard array syntax would have given strange compiler errors on that line of code. – PaulMcKenzie May 21 '22 at 11:31
  • Thanks a lot Paul, the syntax looks correct now. But I still need to correct the logic in the code ig, cuz I am not getting the intended result. – Anonymousstriker38596 May 21 '22 at 11:56
  • *Trying to Find Error in my 900 Rated Codeforces Question Code* -- [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – PaulMcKenzie May 21 '22 at 12:46

0 Answers0