Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3351 - Sum of Good Subsequences.

    Hard

    You are given an integer array nums. A good subsequence is defined as a subsequence of nums where the absolute difference between any two consecutive elements in the subsequence is exactly 1.

    Return the sum of all possible good subsequences of nums.

    Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.

    Note that a subsequence of size 1 is considered good by definition.

    Example 1:

    Input: nums = 1,2,1

    Output: 14

    Explanation:

    • Good subsequences are: [1], [2], [1], [1,2], [2,1], [1,2,1].

    • The sum of elements in these subsequences is 14.

    Example 2:

    Input: nums = 3,4,5

    Output: 40

    Explanation:

    • Good subsequences are: [3], [4], [5], [3,4], [4,5], [3,4,5].

    • The sum of elements in these subsequences is 40.

    Constraints:

    • <code>1 <= nums.length <= 10<sup>5</sup></code>

    • <code>0 <= numsi<= 10<sup>5</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer sumOfGoodSubsequences(IntArray nums)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait