Counting points on a given irregular grid that are inside or outside a circle Given some points that are represented as (X, Y) coordinates, my task is to count the points that are inside a circle, on the circle, and outside the circle.

The input is given in the following way: first, the number n is provided. Then, n numbers are inputted that represent the SetX. Afterwards, the numbers for m and SetY are given in the same way.

The points that I should consider are Cartesian product of SetX and SetY. For example, given SetX: {0, 3, 4} and SetY: {1, 4}, the points would be: (0, 1), (0, 4), (3, 1), (3, 4), (4, 1), (4, 4).

The maximum element of SetX is guaranteed to be the same as the maximum element in SetY, and must be an even number. The center of previously mentioned circle is determined as (max/2, max/2), with radius max/2.

Additional limitations: n and m are at least 1 and no more than 107. Elements of SetX and SetY are no greater than 107.

My solution is as follows:

#include &lt;iostream&gt;

inline double distance_squared(const unsigned int&amp;, const unsigned int&amp;);

unsigned int center = 0;

int main()
{
    unsigned int n, m, i, inside = 0, on_circut = 0, outside = 0, tmp, distance_sqr;

    std::cin &gt;&gt; n;

    unsigned int set_X[n];
    for(i = 0; i &lt; n; ++i){
        std::cin &gt;&gt; set_X[i]; // reading the set of Xs
        if(set_X[i] &gt; center) center = set_X[i]; // finding maximum
    }
    center = center &gt;&gt; 1; //div by 2

    std::cin &gt;&gt; m;

    while(m--){ // reading elements of Y one by one
        std::cin &gt;&gt; tmp; //
        for(i = 0; i &lt; n; ++i){ // for each pair of tmp (being Y coordinate) and a number from Xs (being coord. X)...
            distance_sqr = distance_squared(set_X[i], tmp); // find the distance^2 and compare it to radius^2
            if(distance_sqr == center*center){ ++on_circut; continue; }
            if(distance_sqr &gt; center*center) ++outside;
            else ++inside;
        }
    }

    std::cout &lt;&lt; inside &lt;&lt; ' ' &lt;&lt; on_circut &lt;&lt; ' ' &lt;&lt; outside;

    return 0;

}

inline double distance_squared(const unsigned int&amp; x, const unsigned int&amp; y){
    return (x - center)*(x - center) + (y - center)*(y - center);
}


The solution provides correct output, but it appears to be too slow. The online judge gives the "time limit exceeded" on majority of the tests. Please do note that in the original code I do not use std::cin and std::cout, but custom functions that I omitted, since I am certain that they are significantly faster than iostream functions, plus, they are quite long compared to the rest of the program.

Is there any other way I could optimise the code? Is my approach generally bad in terms of time complexity?
Well, you are manually testing every point \$(X_i, Y_j)\$ to see where it falls. You shouldn't, really.
Remember the sage advice: If in doubt, sort.

A better algorithm goes:


Read all X-coordinates.
Change them to squared distance from center-line.
Sort them.
Read an Y-coordinate.
Calculate the squared-distance from center-line for X-coordinates leading to the point sitting on the circle. \$\Delta x^2 = r^2 - (y - r)^2\$
Use binary search to determine how many elements from point 3 are smaller/equal/greater than the target.
Repeat at 4 until done.


Your algorithm was \$O(n \cdot m)\$. This one is \$O((n + m) \cdot \log(n))\$.

As an aside, if you simply double the given coordinates before processing them, you don't have to insist on the largest being even.



Now let's take a look at your code:


unsigned only has a minimal maximum of \$2^{16}-1\$, less than \$10^7\$. Even if it had 32 value-bits as on most desktops, \$2^{32}-1\$ is still below \$10^{14}\$. So, use long long which has a guaranteed 64 value-bits.
Why do you use double as the result of distance_squared()? If you did the calculation with double it would be accurate, though slow, but you only convert implicitly on return, which is far too late to help.
An unsigned is cheaply copied. Passing it by constant reference is actually a pessimization, luckily the optimizer will inline it so no harm done.
return 0; is implicit for main().
You should always finish your output with a newline. Not doing so means the prompt after calling the program won't be on a new line, and on ancient systems could have even less desirable outcomes.
The one time you use continue; it doesn't actually have any effect.
Some people like putting single-statements on the same line as the corresponding if, else if or else. I would desist as it impairs readability.
All else being equal, try to define any variable in the smallest scope you can, so a reader doesn't have to trace it all over the place. Concentration is a resource best preserved where possible.
inline only has the effect of allowing multiple equivalent definitions left. If it's only used in the current TU, better mark it static to get internal linkage.
Also consider defining a function before its use so you don't have to add a separate declaration. Remember, don't repeat yourself (DRY).
Restrict line-length so there's no horizontal scrolling. Horizontal scrolling, especially couple with vertical scrolling, is the bane of readability.