package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**
 * 446. Arithmetic Slices II - Subsequence
 *
 * A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

 For example, these are arithmetic sequences:

 1, 3, 5, 7, 9
 7, 7, 7, 7
 3, -1, -5, -9

 The following sequence is not arithmetic.

 1, 1, 2, 5, 7

 A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.

 A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.

 The function should return the number of arithmetic subsequence slices in the array A.

 The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.


 Example:

 Input: [2, 4, 6, 8, 10]

 Output: 7

 Explanation:
 All arithmetic subsequence slices are:
 [2,4,6]
 [4,6,8]
 [6,8,10]
 [2,4,6,8]
 [4,6,8,10]
 [2,4,6,8,10]
 [2,6,10]
 */
public class _446 {
    /**reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution*/
    public int numberOfArithmeticSlices(int[] A) {
        int res = 0;
        Map<Integer, Integer>[] map = new Map[A.length];

        for (int i = 0; i < A.length; i++) {
            map[i] = new HashMap<>(i);

            for (int j = 0; j < i; j++) {
                long diff = (long) A[i] - A[j];
                if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) {
                    continue;
                }

                int d = (int) diff;
                int c1 = map[i].getOrDefault(d, 0);
                int c2 = map[j].getOrDefault(d, 0);
                res += c2;
                map[i].put(d, c1 + c2 + 1);
            }
        }

        return res;
    }
}

@@ -1234,19 +1234,71 @@
 6 %7B%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 referenc
@@ -1385,19 +1385,33 @@
 solution
-*/%0A
+%0A         */%0A    
     publ
@@ -1457,24 +1457,28 @@
 ) %7B%0A        
+    
 int res = 0;
@@ -1486,16 +1486,20 @@
         
+  
+  
 Map%3CInte
@@ -1540,32 +1540,36 @@
 ngth%5D;%0A%0A        
+    
 for (int i = 0; 
@@ -1597,24 +1597,28 @@
             
+   
+ 
 map%5Bi%5D = new
@@ -1633,16 +1633,20 @@
 %3C%3E(i);%0A%0A
+    
         
@@ -1675,24 +1675,28 @@
 %3C i; j++) %7B%0A
+    
             
@@ -1747,16 +1747,20 @@
         
+    
 if (diff
@@ -1832,16 +1832,20 @@
         
+    
 continue
@@ -1854,35 +1854,43 @@
                 
+ 
-%7D%0A%0A
+   %7D%0A%0A    
                 
@@ -1921,24 +1921,28 @@
             
+  
+  
 int c1 = map
@@ -1957,32 +1957,36 @@
 rDefault(d, 0);%0A
+    
                 
@@ -2037,16 +2037,20 @@
         
+   
+ 
 res += c
@@ -2052,16 +2052,20 @@
  += c2;%0A
+    
         
@@ -2100,16 +2100,20 @@
 2 + 1);%0A
+    
         
@@ -2118,35 +2118,43 @@
       %7D%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         return r
@@ -2157,16 +2157,26 @@
 rn res;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 446