![]() |
VOOZH | about |
Given two integers S and D, the task is to count the number of Arithmetic Progressions possible with sum S and common difference D.
Examples:
Input: S = 12, D = 1
Output: 4
Explanation: Following 4 arithmetic progressions with sum 12 and common difference 1 are possible:
- {12}
- {3, 4, 5}
- {-2, -1, 0, 1, 2, 3, 4, 5}
- {-11, -10, -9, ..., 10, 11, 12}
Input: S = 1, D = 1
Output: 2
Explanation: Following 2 arithmetic progressions with sum 1 and common difference 1 are possible:
- {1}
- {0, 1}
Approach: The given problem can be solved based on the following observations:
where,
S is the sum of the AP series,
a is the first term of the series,
N is the number of terms in the series,
d is a common difference
=> 2*S = N*(2*a + (N - 1)*d) ... (1)
=> ...(2)
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
4
Time Complexity: O(sqrt(S))
Auxiliary Space: O(1), since no extra space has been taken.