Skip to content

Instantly share code, notes, and snippets.

@DeoEsor
Created May 18, 2022 12:59
Show Gist options
  • Save DeoEsor/417bb05e86c380b38f1279bcdd7c884b to your computer and use it in GitHub Desktop.
Save DeoEsor/417bb05e86c380b38f1279bcdd7c884b to your computer and use it in GitHub Desktop.
For interview
namespace Codity;
public class NumberOfDiscIntersections
{
const int MaxIntersectingPairs = 10000000;
public int solution(int[] A)
{
var result = 0;
int[] left = new int[A.Length],
right = new int[A.Length];
for (int i = 0, j = A.Length - 1; i < A.Length; i++)
{
left[i > A[i]? i - A[i]: 0]++;
right[j - i > A[i]? i + A[i]: j]++;
}//TODO Parallel.For
var temp = 0;
for (var i = 0; i < A.Length; i++)
{
if (left[i] > 0)
{
result += temp * left[i];
result += left[i] * (left[i] - 1) / 2;
if (result > MaxIntersectingPairs)
return -1;
temp += left[i];
}
temp -= right[i];
}//TODO Paralell.For
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment