I am working a programming puzzle and I am a little baffled. The problem is count the number of intersections of disks on a 2 dimensional plan. x is always 0 and y is index into the array. The radius is the element is stored in the array element. Performance is fine with the number of elements a small number of elements, but with a large number of elements like 10,000,000 performance is bad. I am currently using 2 nested for loop to process the arrays.
I would appreciate any help someone might give. I am tied to data structures given to me and can not change them. The calculation if the disk intersects another since I am dealing with integers and the center points are on the same y axis.
Below is the code:
int number_of_intersections ( int A[], int n )
{
int base = 0;
int inc = 0;
int intersect = 0;
int maxrad = 0;
int x;
if (n>1)
{
for (base=0;base<(n-1);base++)
{
inc = base+1;
do
{
if ( inc - base <= (A[base] + A[inc]))
{
intersect ++;
if (!(intersect^10000000))
{
return -1;
}
}
inc ++;
} while (inc < n);
}
}
return intersect;
}
Thank you for your time
if (!(intersect ^ 1000000)). Just writeif (intersect == 10000000). It's way clearer. Also, can you provide some more context and examples here? What have you tried? Why didn't it work?for(inc=base+1; inc<n; ++inc)Atransport the necessary information about tuples ofyand radius? IsAsupposed to be sorted in some way? If you really want to get rid of a quadratic algorithm you have to get things clean, specification and code. Don't useintas an all purpose data type. Indices should besize_tradii probablydoubleor so.