java.lang.Object
g0401_0500.s0446_arithmetic_slices_ii_subsequence.Solution

public class Solution extends Object
446 - Arithmetic Slices II - Subsequence.<p>Hard</p> <p>Given an integer array <code>nums</code>, return <em>the number of all the <strong>arithmetic subsequences</strong> of</em> <code>nums</code>.</p> <p>A sequence of numbers is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1, 3, 5, 7, 9]</code>, <code>[7, 7, 7, 7]</code>, and <code>[3, -1, -5, -9]</code> are arithmetic sequences.</li> <li>For example, <code>[1, 1, 2, 5, 7]</code> is not an arithmetic sequence.</li> </ul> <p>A <strong>subsequence</strong> of an array is a sequence that can be formed by removing some elements (possibly none) of the array.</p> <ul> <li>For example, <code>[2,5,10]</code> is a subsequence of <code>[1,2,1, **2** ,4,1, **5** , **10** ]</code>.</li> </ul> <p>The test cases are generated so that the answer fits in <strong>32-bit</strong> integer.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,4,6,8,10]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> All arithmetic subsequence slices are:</p> <p>[2,4,6]</p> <p>[4,6,8]</p> <p>[6,8,10]</p> <p>[2,4,6,8]</p> <p>[4,6,8,10]</p> <p>[2,4,6,8,10]</p> <p>[2,6,10]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [7,7,7,7,7]</p> <p><strong>Output:</strong> 16</p> <p><strong>Explanation:</strong> Any subsequence of this array is arithmetic.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfArithmeticSlices

      public int numberOfArithmeticSlices(int[] arr)