Open In App

Count Arithmetic Progressions having sum S and common difference equal to D

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. {12}
  2. {3, 4, 5}
  3. {-2, -1, 0, 1, 2, 3, 4, 5}
  4. {-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. {1}
  2. {0, 1}

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

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

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

  • 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 2*S and check if there exists any factor F such that the product of F and (2*a + (F – 1)*d) is equal to 2 * S. If found to be true, then count that factor for one of the possible AP having the given sum S.
    • If there exists any factor F, such that (D * F – (2 * S / F) + D) is divisible by 2, then count that factor for one of the possible AP having the given sum S.

Follow the steps below to solve the problem:

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

Below is the implementation of the above approach:

C++

// 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

// 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#

// 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.

Javascript

<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

# 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)) 
Auxiliary Space: O(1),  since no extra space has been taken.
 



Similar Reads

Count Arithmetic Progressions having sum N and common difference equal to 1
Given an integer N, the task is to count all arithmetic progression with common difference equal to 1 and the sum of all its elements equal to N. Examples: Input: N = 12Output: 3Explanation: Following three APs satisfy the required conditions: {3, 4, 5}{−2, −1, 0, 1, 2, 3, 4, 5}{−11, −10, −9, …, 10, 11, 12]} Input: N = 963761198400Output: 1919 Appr
6 min read
Find an integer that is common in the maximum number of given arithmetic progressions
Given two integer arrays A[] and D[], where Ai and Di represent the first element and common difference of an arithmetic progression respectively, the task is to find an element that is common in the maximum number of given arithmetic progressions.Examples: Input: A[] = {3, 1, 2, 5}, D[] = {2, 3, 1, 2} Output: 7 Explanation: The integer 7 is presen
5 min read
K-th term from given N merged Arithmetic Progressions
Given an integer K and an array arr[] of N integers, each of which is the first term and common difference of an Arithmetic Progression, the task is to find the Kth element of the set S formed by merging the N arithmetic progressions. Examples: Input: arr[] = {2, 3}, K = 10 Output: 15 Explanation: There are 2 arithmetic progressions. First-term and
9 min read
Introduction to Arithmetic Progressions | Class 10 Maths
A progression is a sequence or series of numbers in which they are arranged in a particular order such that the relation between the consecutive terms of series or sequence is always constant. In a progression, it is possible to obtain the nth term of the series. In mathematics, there are 3 types of progressions: Arithmetic Progression(AP)Geometric
5 min read
Class 10 RD Sharma Solutions - Chapter 9 Arithmetic Progressions - Exercise 9.4 | Set 1
Question 1. Find: (i) 10th tent of the AP 1, 4, 7, 10…. (ii) 18th term of the AP √2, 3√2, 5√2, ……. (iii) nth term of the AP 13, 8, 3, -2, ………. (iv) 10th term of the AP -40, -15, 10, 35, …………. (v) 8th term of the AP 11, 104, 91, 78, …………… (vi) 11th tenor of the AP 10.0, 10.5, 11.0, 11.2, ………….. (vii) 9th term of the AP 3/4, 5/4, 7/4 + 9/4, ……….. Sol
17 min read
Class 10 RD Sharma Solutions - Chapter 9 Arithmetic Progressions - Exercise 9.4 | Set 2
Question 14. The 4th term of an A.P. is three times the first and the 7th term exceeds twice the third term by 1. Find the first term and the common difference. Solution: Let’s assume that the first term and the common difference of the A.P to be a and d respectively. Given that, 4th term of an A.P. is three times the first a4 = 3(a) As we know tha
10 min read
Largest integer that can be placed at center of given square Matrix to maximise arithmetic progressions
Given a N x N matrix, such that the element at the index [N/2, N/2] is missing, the task is to find the maximum integer that can be placed at index [N/2, N/2] such that the count of arithmetic progressions over all rows, columns, and diagonals is maximized. Example: Input: mat[][]={{3, 4, 11}, {10, ?, 9}, {-1, 6, 7}}Output: 5Explanation: The maximu
12 min read
Optimizing Arithmetic Progressions in Subsequences
Given a string S, a string T is considered good if following 2 conditions hold: T is a subsequence of SThe indices of the subsequence T in S forms an arithmetic progression For example, if S = "aaabb" then T = "aab" is good because T is subsequence of S at indices 1,3,5 which is an arithmetic progression. The task is to determine any subsequence th
8 min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's &ge; count of 0's in each prefix substring
Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of length 2 are {"00", "01", "10" and "11"}. Out of
7 min read
Arithmetic Progression - Common difference and Nth term | Class 10 Maths
Arithmetic Progression is a sequence of numbers where the difference between any two successive numbers is constant. For example 1, 3, 5, 7, 9....... is in a series which has a common difference (3 - 1) between two successive terms is equal to 2. If we take natural numbers as an example of series 1, 2, 3, 4... then the common difference (2 - 1) bet
5 min read