package com.johnny.java;

import java.util.Arrays;

/**
 * Created by Johnny on 2/13/2017.
 */
public class NumberOfDiscIntersections {
    public static void main(String[] args) {
        int[] a = {1, 1, 1, 1};
        NumberOfDiscIntersections n = new NumberOfDiscIntersections();
        System.out.println(n.solution(a));
        System.out.println(n.solution(a));
    }

    public int solution(int[] A) {
        int result = 0;
        int j = 0;
        long[] upper = new long[A.length];
        long[] lower = new long[A.length];

        for (int i = 0; i < A.length; i++) {
            lower[i] = (long) i - A[i];
            upper[i] = (long) i + A[i];
        }

        Arrays.sort(lower);
        Arrays.sort(upper);

        for (int i = 0; i < A.length; i++) {
            while (j < A.length && upper[i] >= lower[j]) {
                result += j - i;
                j++;
            }
            if (result > 10000000) return -1;
        }

        return result;
    }
}

@@ -196,23 +196,29 @@
 a = %7B1, 
-1, 1, 1
+2147483647, 0
 %7D;%0A     
@@ -355,32 +355,33 @@
 intln(n.solution
+B
 (a));%0A    %7D%0A%0A   
@@ -992,11 +992,455 @@
 t;%0A    %7D
+%0A%0A    //O(N*N) Low Performance.%0A    public int solutionB(int%5B%5D A) %7B%0A        int result = 0;%0A        for (int i = 0; i %3C A.length - 1; i++) %7B%0A            for (int k = i + 1; k %3C A.length; k++) %7B%0A                if (i + (long) A%5Bi%5D %3E= k) %7B%0A                    result = result + 1;%0A                %7D else if (i + (long) A%5Bi%5D %3E= k - (long) A%5Bk%5D) %7B%0A                    result++;%0A                %7D%0A            %7D%0A        %7D%0A        return result;%0A    %7D
 %0A%7D%0A

Implement another low performance method.