We draw N discs on a plane. The discs are numbered from 0 to N − 1. An array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius A[J].
We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th and K-th discs have at least one common point (assuming that the discs contain their borders).
The figure below shows discs drawn for N = 6 and A as follows:
A[0] = 1 A[1] = 5 A[2] = 2 A[3] = 1 A[4] = 4 A[5] = 0There are eleven (unordered) pairs of discs that intersect, namely:
- discs 1 and 4 intersect, and both intersect with all the other discs;
- disc 2 also intersects with discs 0 and 3.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A describing N discs as explained above, returns the number of (unordered) pairs of intersecting discs. The function should return −1 if the number of intersecting pairs exceeds 10,000,000.
Given array A shown above, the function should return 11, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [0..2,147,483,647].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
long[] dps = new long[a.Length];
long[] dpe = new long[a.Length];
for (int i = 0; i < a.Length; i++)
{
dps[Math.max(0, i - a[i])]++;
dpe[Math.min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
Compilation failed: 6 error(s), 0 warnings Solution.cs(16,18): error CS0117: `System.Math' does not contain a definition for `max' /opt/codility-mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(16,35): error CS0584: Internal compiler error: Object reference not set to an instance of an object Solution.cs(16,35): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement Solution.cs(17,18): error CS0117: `System.Math' does not contain a definition for `min' /opt/codility-mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(17,46): error CS0584: Internal compiler error: Object reference not set to an instance of an object Solution.cs(17,46): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
long[] dps = new long[a.Length];
long[] dpe = new long[a.Length];
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
long[] dps = new long[a.Length];
long[] dpe = new long[a.Length];
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
[1,int.MaxValue, 0]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
long[] dps = new long[a.Length];
long[] dpe = new long[a.Length];
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
[1, 2147483647, 0]
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
HashSet<long> dps = new HashSet<long>();
HashSet<long> dpe = new HashSet<long>();
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
Compilation failed: 7 error(s), 0 warnings Solution.cs(16,12): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(17,12): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(23,16): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(25,30): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(26,26): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(27,21): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>' Solution.cs(31,17): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.HashSet<long>'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
if (dps[i] > 0)
{
result += t * dps[i];
result += dps[i] * (dps[i] - 1) / 2;
t += dps[i];
if (result > 10000000)
return -1;
}
t -= dpe[i];
}
return (int)result;
}
}
[1, 2147483647, 0]
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
int value;
if (dps.TryGetValue(i, out value))
{
result += t * value;
result += value * (value - 1) / 2;
t += value;
if (result > 10000000)
return -1;
}
dpe.TryGetValue(i, out value)
t -= value;
}
return (int)result;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(33,8): error CS1525: Unexpected symbol `t'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
dps[Math.Max(0, i - a[i])]++;
dpe[Math.Min(a.Length - 1, i + a[i])]++;
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
int value;
if (dps.TryGetValue(i, out value))
{
result += t * value;
result += value * (value - 1) / 2;
t += value;
if (result > 10000000)
return -1;
}
dpe.TryGetValue(i, out value);
t -= value;
}
return (int)result;
}
}
[1, 2147483647, 0]
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.Int64,System.Int32].get_Item (Int64 key) [0x00000] in <filename unknown>:0 at Solution.solution (System.Int32[] a) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
inc(dps, Math.Max(0, i - a[i]));
inc(dpe, Math.Min(a.Length - 1, i + a[i]));
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
int value;
if (dps.TryGetValue(i, out value))
{
result += t * value;
result += value * (value - 1) / 2;
t += value;
if (result > 10000000)
return -1;
}
dpe.TryGetValue(i, out value);
t -= value;
}
return (int)result;
}
private static void inc(Dictionary<long, int> values, long index)
{
int value;
values.TryGetValue(index, out value);
values[index] = ++value;
}
}
[1, 2147483647, 0]
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
inc(dps, Math.Max(0, i - a[i]));
inc(dpe, Math.Min(a.Length - 1, i + a[i]));
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
int value;
if (dps.TryGetValue(i, out value))
{
result += t * value;
result += value * (value - 1) / 2;
t += value;
if (result > 10000000)
return -1;
}
dpe.TryGetValue(i, out value);
t -= value;
}
return (int)result;
}
private static void inc(Dictionary<long, int> values, long index)
{
int value;
values.TryGetValue(index, out value);
values[index] = ++value;
}
}
[1, 2147483647, 0]
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] a) {
long result = 0;
Dictionary<long, int> dps = new Dictionary<long, int>();
Dictionary<long, int> dpe = new Dictionary<long, int>();
for (int i = 0; i < a.Length; i++)
{
inc(dps, Math.Max(0, i - a[i]));
inc(dpe, Math.Min(a.Length - 1, i + a[i]));
}
long t = 0;
for (int i = 0; i < a.Length; i++)
{
int value;
if (dps.TryGetValue(i, out value))
{
result += t * value;
result += value * (value - 1) / 2;
t += value;
if (result > 10000000)
return -1;
}
dpe.TryGetValue(i, out value);
t -= value;
}
return (int)result;
}
private static void inc(Dictionary<long, int> values, long index)
{
int value;
values.TryGetValue(index, out value);
values[index] = ++value;
}
}
The solution obtained perfect score.