Count of ways to generate Sequence of distinct consecutive odd integers with sum N
Last Updated :
10 Mar, 2022
Given an integer N, the task is to find the total number of ways a sequence can be formed consisting of distinct consecutive odd integers that add up to N.
Examples:
Input: N = 45
Output: 3
Explanation: 3 ways to choose distinct consecutive odd numbers that add up to 45 are –
{5, 7, 9, 11, 13}, {13, 15, 17} and {45}.
Input : N = 20
Output : 1
Explanation: 9 and 11 are the only consecutive odd numbers whose sum is 20
Approach: The idea to solve the problem is based on the idea of sum of first K consecutive odd integers:
- The sum of first K consecutive odd integers is K2.
- Let there be a sequence of consecutive odd integers from (y+1)th odd number to xth odd number (x > y), whose sum is N.
- Then, x2 – y2 = N or (x + y) * (x – y) = N.
- Let a and b be two divisors of N. Therefore, a * b=N.
- Hence, x + y = a & x – y = b
- Solving these two, we get x = (a + b) / 2.
- This implies, if (a + b) is even, then x and y would be integral, which means there would exist a sequence of consecutive odd integers that adds up to N.
Follow the steps mentioned below to implement the above observation:
- Iterate through all pairs of divisors, such that their product is N.
- If the sum of such a pair of divisors is even, increment the count of answer by 1.
- Return the final count at the end.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfSequences( int N)
{
int count = 0;
for ( int i = 1; i * i <= N; i++) {
if (N % i == 0) {
int divisor1 = i;
int divisor2 = N / i;
int sum = divisor1 + divisor2;
if (sum % 2 == 0) {
count++;
}
}
}
return count;
}
int main()
{
int N = 45;
int number_of_sequences = numberOfSequences(N);
cout << number_of_sequences;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int numberOfSequences( int N)
{
int count = 0 ;
for ( int i = 1 ; i * i <= N; i++) {
if (N % i == 0 ) {
int divisor1 = i;
int divisor2 = N / i;
int sum = divisor1 + divisor2;
if (sum % 2 == 0 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 45 ;
int number_of_sequences = numberOfSequences(N);
System.out.print(number_of_sequences);
}
}
|
Python3
import math
def numberOfSequences(N):
count = 0 ;
for i in range ( 1 ,math.ceil(math.sqrt(N))):
if (N % i = = 0 ):
divisor1 = i;
divisor2 = N / / i;
sum = divisor1 + divisor2;
if ( sum % 2 = = 0 ):
count = count + 1
return count;
N = 45 ;
number_of_sequences = numberOfSequences(N);
print (number_of_sequences);
|
C#
using System;
class GFG {
static int numberOfSequences( int N)
{
int count = 0;
for ( int i = 1; i * i <= N; i++) {
if (N % i == 0) {
int divisor1 = i;
int divisor2 = N / i;
int sum = divisor1 + divisor2;
if (sum % 2 == 0) {
count++;
}
}
}
return count;
}
public static void Main()
{
int N = 45;
int number_of_sequences = numberOfSequences(N);
Console.Write(number_of_sequences);
}
}
|
Javascript
<script>
const numberOfSequences = (N) => {
let count = 0;
for (let i = 1; i * i <= N; i++) {
if (N % i == 0) {
let divisor1 = i;
let divisor2 = parseInt(N / i);
let sum = divisor1 + divisor2;
if (sum % 2 == 0) {
count++;
}
}
}
return count;
}
let N = 45;
let number_of_sequences = numberOfSequences(N);
document.write(number_of_sequences);
</script>
|
Time Complexity: O(√N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...