# Count Double Increasing Series in A Range in Java

Two integers P and Q, are given to us. The task is to find the total count of a series whose current element is double or more than double than the last occurred element of the series such that any element of the series can not exceed P. It can be equal to P.

Example 1:

Input

int P = 9, int Q = 3

Output: 14

Explanation: There are total 14 series whose size is 3, and the current element is twice or more than twice than the last occurred element of the series such that each element of the series is less than or equal to 9. All those series are: {1, 2, 4}, {1, 2, 5}, {1, 2, 6}, {1, 2, 7}, {1, 2, 8}, {1, 2, 9}, {1, 3, 6}, {1, 3, 7}, {1, 3, 8}, {1, 3, 9}, {1, 4, 8}, {1, 4, 9}, {2, 4, 8}, {2, 4, 9}

Example 2:

Input

int P = 12, int Q = 4

Output: 10

Explanation: There are total 10 series whose size is 4, and the current element is twice or more than twice than the last occurred element of the series such that each element of the series is less than or equal to 12. All those series are: {1, 2, 4, 8}, {1, 2, 4, 9}, {1, 2, 4, 10}, {1, 2, 4, 11}, {1, 2, 4, 12}, {1, 2, 5, 10}, {1, 2, 5, 11}, {1, 2, 5, 12}, {1, 2, 6, 12}, {1, 3, 6, 12}

Example 3:

Input

int P = 15, int Q = 7

Output: 0

Explanation: There is no series that will be of size 7 whose current element is twice or more than twice the previous element such that each element will be less than or equal to 15. Hence, the output is 0.

## Naïve Approach:

In the naïve approach, we will use recursion in a top-down manner to find the solution.

FileName: DoubleIncSeq.java

Output:

```There are total 14 series whose elements are <= 9 and are of size: 3

There are total 10 series whose elements are <= 12 and are of size: 4

There is no series whose elements are <= 15 and is of size: 7
```

Complexity Analysis: One recursive call is giving birth to two more recursive calls. Hence, the time complexity of the program is exponential.

Because of the exponential time complexity, the above program is not suitable for larger inputs. Hence, it is required to do some optimization in order to reduce the time complexity.

## Approach: Recursion with Memoization

There are many subproblems in the above program that are solved again and again. Therefore, it is required to avoid the repetition of the subproblems with the help of a two-dimensional array that stores the answer to the computed subproblems. The illustration of the same is mentioned in the following program.

FileName: DoubleIncSeq1.java

Output:

```There are total 14 series whose elements are <= 9 and are of size: 3

There are total 10 series whose elements are <= 12 and are of size: 4

There is no series whose elements are <= 15 and is of size: 7
```

Complexity Analysis: Because of the memoization, the time complexity of the program is O(p x q). Also, the program is using an auxiliary array for storing the answer of the subproblem, making the space complexity of the program O(p x q), where p is the maximum value an element can have in the required series and q is the total count of the series found.

As it is a known fact that the iterative approach is always better than the recursive approach, we will try to write the above program using loops.

FileName: DoubleIncSeq2.java

Output:

```There are total 14 series whose elements are <= 9 and are of size: 3

There are total 10 series whose elements are <= 12 and are of size: 4

There is no series whose elements are <= 15 and is of size: 7
```

Complexity Analysis: The time, as well as space complexity of the program is the same as the previous program.