Given two integers ** S** and

**, the task is to count the number of Arithmetic Progressions possible with sum**

**D****and common difference**

**S****.**

**D****Examples:**

S = 12, D = 1Input:4Output:Following 4 arithmetic progressions with sum 12 and common difference 1 are possible:Explanation:

- {12}
- {3, 4, 5}
- {-2, -1, 0, 1, 2, 3, 4, 5}
- {-11, -10, -9, …, 10, 11, 12}

S = 1, D = 1Input:2Output:Following 2 arithmetic progressions with sum 1 and common difference 1 are possible:Explanation:

- {1}
- {0, 1}

** Approach:** The given problem can be solved based on the following observations:

- The sum of the AP series is given by:

[Tex]S = \frac{N}{2}*(2*a + (N – 1)*d)[/Tex]

where,

is the sum of the AP series,Sis the first term of the series,ais the number of terms in the series,Nis a common differenced

- After rearranging the above expressions:

=> 2*S = N*(2*a + (N – 1)*d) … (1)

=>[Tex]a = \frac{\frac{2*S}{N} – (N – 1)*d}{2} [/Tex] …(2)

- From the above two expressions:
- The idea is to consider all the factors of
and check if there exists any factor**2*S**such that the product of**F**and**F**is equal to**(2*a + (F – 1)*d)**. If found to be true, then count that factor for one of the possible**2 * S**having the given sum**AP**.**S** - If there exists any factor
, such that**F**is divisible by**(D * F – (2 * S / F) + D)**, then count that factor for one of the possible**2**having the given sum**AP**.**S**

- The idea is to consider all the factors of

Follow the steps below to solve the problem:

- Initialize a variable, say
, to store the count of**answer**s with sum**AP**and common difference**S**.**D** - Iterate over the range
and check if**[1, ?2*S]**is divisible by**2 * S**, then perform the following steps:**i**- If the value of
is divisible by**((2 * S / i) + 1 – i * D)**, then increment**2**by**answer**.**1** - If the value of
is divisible by**(i * D – S / i + 1)**, then increment**2**by**answer**.**1**

- If the value of
- After completing the above steps, print the value of
as the resultant count of**answer**s.**AP**

Below is the implementation of the above approach:

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of APs
// with sum S and common difference D
int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver Code
int main()
{
int S = 12, D = 1;
cout << countAPs(S, D);
return 0;
}

// Java program for above approach
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Function to count the number of APs
// with sum S and common difference D
static int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= Math.sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver code
public static void main(String[] args)
{
int S = 12, D = 1;
System.out.println(countAPs(S, D));
}
}
// This code is contributed by susmitakundugoaldanga.

// C# program for the above approach
using System;
class GFG{
// Function to count the number of APs
// with sum S and common difference D
static int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= Math.Sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver code
static void Main()
{
int S = 12, D = 1;
Console.Write(countAPs(S, D));
}
}
// This code is contributed by sanjoy_62.

<script>
// Javascript program for the above approach
// Function to count the number of APs
// with sum S and common difference D
function countAPs(S, D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
let answer = 0;
// Iterate over the factors of 2*S
for (let i = 1; i <= Math.sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver code
let S = 12, D = 1;
document.write(countAPs(S, D));
</script>

# Python3 program for the above approach
# Function to count the number of APs
# with sum S and common difference D
def countAPs(S, D):
# Multiply S by 2
S = S * 2
# Stores the count of APs
answer = 0
# Iterate over the factors of 2*S
for i in range(1, S):
if i * i > S:
break
# Check if i is the factor
# or not
if (S % i == 0):
# Conditions to check if AP
# can be formed using factor F
if (((S // i) - D * i + D) % 2 == 0):
answer += 1
if ((D * i - (S // i) + D) % 2 == 0):
answer += 1
# Return the total count of APs
return answer
# Driver Code
if __name__ == '__main__':
S, D = 12, 1
print(countAPs(S, D));
# This code is contributed by mohit kumar 29.

**Output**

4

** Time Complexity:** O(sqrt(S))

**O(1), since no extra space has been taken.**

**Auxiliary Space:**