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 = 12
Output: 3
Explanation:
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 = 963761198400
Output: 1919
Approach: The given problem can be solved using the following properties of AP:
- The sum of an AP series having N terms with first term as A and common difference 1 is given by the formula S = (N / 2 )* ( 2 *A + N − 1).
- Solving the above equation:
S = N / 2 [2 *A + N − 1].
Rearranging this and multiplying both sides by 2
=> 2 * S = N * (N + 2 * A − 1)
Rearranging further
=> A = ((2 * N / i ) − i + 1) / 2.
Now, iterate over all the factors of 2 * N, and check for each factor i, whether (2 * N/i) − i + 1 is even or not.
Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the number of AP series possible having given conditions.
- Iterate through all the factors of 2 * N and for every ith factor, check if (2 * N / i) − i + 1 is even.
- If found to be true, increment the count.
- Print count – 1 as the required answer, as the sequence consisting only of N needs to be discarded.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void countAPs( long long int N)
{
long long int count = 0;
for ( long long int i = 1;
i * i <= 2 * N; i++) {
long long int res = 2 * N;
if (res % i == 0) {
long long int op = res / i - i + 1;
if (op % 2 == 0) {
count++;
}
if (i * i != res
and (i - res / i + 1) % 2 == 0) {
count++;
}
}
}
cout << count - 1 << "\n" ;
}
int main()
{
long long int N = 963761198400;
countAPs(N);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void countAPs( long N)
{
long count = 0 ;
for ( long i = 1 ; i * i <= 2 * N; i++) {
long res = 2 * N;
if (res % i == 0 ) {
long op = res / i - i + 1 ;
if (op % 2 == 0 ) {
count++;
}
if (i * i != res
&& (i - res / i + 1 ) % 2 == 0 ) {
count++;
}
}
}
System.out.println(count - 1 );
}
public static void main(String[] args)
{
long N = 963761198400L;
countAPs(N);
}
}
|
Python3
def countAPs(N) :
count = 0
i = 1
while (i * i < = 2 * N) :
res = 2 * N
if (res % i = = 0 ) :
op = res / i - i + 1
if (op % 2 = = 0 ) :
count + = 1
if (i * i ! = res
and (i - res / i + 1 ) % 2 = = 0 ) :
count + = 1
i + = 1
print (count - 1 )
N = 963761198400
countAPs(N)
|
C#
using System;
public class GFG
{
static void countAPs( long N)
{
long count = 0;
for ( long i = 1; i * i <= 2 * N; i++) {
long res = 2 * N;
if (res % i == 0) {
long op = res / i - i + 1;
if (op % 2 == 0) {
count++;
}
if (i * i != res
&& (i - res / i + 1) % 2 == 0) {
count++;
}
}
}
Console.WriteLine(count - 1);
}
public static void Main(String[] args)
{
long N = 963761198400L;
countAPs(N);
}
}
|
Javascript
<script>
function countAPs(N)
{
let count = 0;
for (let i = 1; i * i <= 2 * N; i++) {
let res = 2 * N;
if (res % i == 0) {
let op = res / i - i + 1;
if (op % 2 == 0) {
count++;
}
if (i * i != res
&& (i - res / i + 1) % 2 == 0) {
count++;
}
}
}
document.write(count - 1);
}
let N = 963761198400;
countAPs(N);
</script>
|
Time Complexity: O(√N)
Auxiliary Space: O(1)