Intuition
Since we want a subsequences whose minimum and maximum element add up to less than or equal to target
- we can just sort
the array and use two pointers on both ends shrinking toward the middle.
This way we have access to the maximum and minimum element at the same time. A[left]
will be the minimum number, A[right]
will be the maximum number and A[left:right]
will be our desired subsequence.
But the thing if A[left:right]
is our desired subarray because A[left] + A[right] <= target
, all elements and subsequences inside this subarray will also fulfill the criteria and must be taken into consideration.
For eg.
So this reduces the problem to a counting problem. The number of subsequences in a range of length (right - left)
is
OR Python-ically speaking, pow(2, right-left, MOD)
where MOD is (10**9 + 7) and not mandatory. According to Python docs, pow(2, right-left, MOD)
is more performant than pow(2, right-left) % MOD
.
PS: This problem is very similar to the famous 1 Two Sum - take a look at that too.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since sort the array in the beginning.
-
Memory
The memory usage is since we are not using any extra data structure.
— A