C# Number Of Disc Intersections
static long NumberOfDiscIntersections(int[] a)
{
long sum = 0;
long active = 0;
var c = a.Length;
var start = new int[c];
var end = new int[c];
for (int k = 0; k < c; k++)
{
var v = a[k];
var startKey = k < v ? 0 : k - v;
start[startKey]++;
var endKey = (long)k + v >= c ? c - 1 : k + v;
end[(int)endKey]++;
}
for (int k = 0; k < c; k++)
{
sum += active * start[k] + (long)start[k] * (start[k] - 1) / 2;
active += start[k] - end[k];
if (sum > 10000000)
{
return -1;
}
}
return sum;
}
This sorts disc start and end points and counts active overlaps without comparing every pair directly.
C# Odd Occurrences In Array
static int? OddOccurrencesInArray(int[] a)
{
var count = new Dictionary<int, int>();
foreach (var value in a)
{
if (!count.ContainsKey(value))
{
count[value] = 1;
}
else
{
count.Remove(value);
}
}
return count.Count > 0 ? count.Keys.First() : (int?)null;
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
C# Palindrome Rearranging
static bool PalindromeRearranging(string inputString)
{
var counts = new Dictionary<char, int>();
foreach (var ch in inputString)
{
counts[ch] = counts.GetValueOrDefault(ch) + 1;
}
var oddCount = counts.Values.Count(v => v % 2 != 0);
return oddCount <= 1;
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
C# Passing Cars
static long PassingCars(int[] a)
{
long passingCars = 0;
long multiply = 0;
foreach (var i in a)
{
if (i == 0)
{
multiply++;
}
else if (multiply > 0)
{
passingCars += multiply;
if (passingCars > 1000000000)
{
return -1;
}
}
}
return passingCars;
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
C# Peaks
static int Peaks(int[] a)
{
var n = a.Length;
if (n <= 2)
{
return 0;
}
var sum = new int[n];
var last = -1;
var dist = 0;
for (int i = 1; i + 1 < n; i++)
{
sum[i] = sum[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1])
{
dist = Math.Max(dist, i - last);
last = i;
sum[i]++;
}
}
sum[n - 1] = sum[n - 2];
if (sum[n - 1] == 0)
{
return 0;
}
dist = Math.Max(dist, n - last);
for (int i = (dist >> 1) + 1; i < dist; i++)
{
if (n % i == 0)
{
last = 0;
int j;
for (j = i; j <= n; j += i)
{
if (sum[j - 1] <= last)
{
break;
}
last = sum[j - 1];
}
if (j > n)
{
return n / i;
}
}
}
for (last = dist; n % last != 0;)
{
last++;
}
return n / last;
}
This finds the peak positions, then tests how many equal blocks can each contain at least one peak.
C# Perm Check
static int PermCheck(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
for (int k = 0; k < sorted.Length; k++)
{
if (k + 1 < sorted.Length && sorted[k] != k + 1)
{
return 0;
}
}
return 1;
}
This validates that every value from 1 to N appears exactly once.
C# Perm Missing Element
static int PermMissingElement(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
for (int k = 0; k < sorted.Length; k++)
{
if (sorted[k] != k + 1)
{
return k + 1;
}
}
return sorted.Length + 1;
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
C# Plagiarism Check
static bool PlagiarismCheck(string[] code1, string[] code2)
{
var c1 = string.Join(" ", code1);
var c2 = string.Join(" ", code2);
if (c1 == c2)
{
return false;
}
var d1 = Regex.Matches(c1, @"\w+");
var d2 = Regex.Matches(c2, @"\w+");
var rCand = new Dictionary<string, string>();
for (int k = 0; k < d1.Count; k++)
{
var v = d1[k].Value;
var w = d2[k].Value;
if (v != w && !double.TryParse(v, out _))
{
rCand[v] = w;
}
}
foreach (var pair in rCand)
{
var orig = Regex.Escape(pair.Key);
c1 = Regex.Replace(c1, $@"(\W){orig}(\W*)", $"$1PLACEHOLDER{pair.Key}$2");
c1 = Regex.Replace(c1, $@"(\W){orig}", $"$1PLACEHOLDER{pair.Key}");
}
foreach (var pair in rCand)
{
var orig = Regex.Escape(pair.Key);
c1 = Regex.Replace(c1, $@"(\W)PLACEHOLDER{orig}(\W)", $"$1{pair.Value}$2");
c1 = Regex.Replace(c1, $@"(\W)PLACEHOLDER{orig}", $"$1{pair.Value}");
}
return c1 == c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
C# Shape Area
static long ShapeArea(long n)
{
return n > 1 ? ShapeArea(n - 1) + 4 * (n - 1) : 1;
}
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.
C# Stone Blocks
static int StoneBlocks(int[] h)
{
var height = new List<int>();
var index = 0;
var blocks = 0;
foreach (var i in h)
{
while (index > 0 && height[index - 1] > i)
{
index--;
}
if (index > 0 && height[index - 1] == i)
{
continue;
}
if (index < height.Count)
{
height[index] = i;
}
else
{
height.Add(i);
}
blocks++;
index++;
}
return blocks;
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.