Find all possible values of K such that the sum of first N numbers starting from K is G
Last Updated :
30 Jul, 2021
Given a positive integer G, the task is to find the number of values of K such that the sum of the first N numbers starting from K is G i.e., (K + (K + 1) + … + (K + N – 1)) = G, where N can be any positive integer.
Examples:
Input: G = 10
Output: 2
Explanation:
Following are the possible values of K:
- K = 1 and N = 4: The sum of {1, 2, 3, 4} is 10(= G).
- K = 10 and N = 1: The sum of {10} is 10(= G).
Therefore, there are two possible values of K. Hence, print 2.
Input: G = 15
Output: 2
Naive Approach: The simplest approach to solve the given problem is to check for each and every value of K from 1 to G and if the given condition is satisfied, then count this value of K. After checking for all possible values of K print the total count.
Time Complexity: O(G2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be solved by observing the mathematical relation that for any value of K:
=> (K) + (K + 1) + (K + 2) + … + (K + N – 1) = G
=> N*K + (1 + 2 + 3 + 4+ … + N – 1) = G
=> G = N*K + (N*(N – 1))/2
Therefore, the value K = G/N – (N – 1)/2. From the above relationship, it can be concluded that the possible value of K exists if and only if:
- N is the factor of G i.e., (G % N) == 0.
- N should be odd i.e., (N % 2) == 1.
Follow the steps below to solve the problem:
- Initialize the variable, say count as 0 that stores the resultant count of values of K.
- Iterate over a range [1, ?G] using the variable i and performing the following tasks:
- If g%i is equal to 0, then if i is not equal to g/i, then if i%2 is equal to 1, then add the value of count by 1 and if (g/i)%2 is equal to 1, then add the value of count by 1.
- Else, if i%2 is equal to 1, then add the value of count by 1.
- After completing the above steps, print the value of the count as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findValuesOfK( int g)
{
int count = 0;
for ( int i = 1; i * i <= g; i++) {
if (g % i == 0) {
if (i != g / i) {
if (i & 1) {
count++;
}
if ((g / i) & 1) {
count++;
}
}
else if (i & 1) {
count++;
}
}
}
cout << count;
}
int main()
{
int G = 125;
findValuesOfK(G);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findValuesOfK( int g)
{
int count = 0 ;
for ( int i = 1 ; i * i <= g; i++) {
if (g % i == 0 ) {
if (i != g / i) {
if (i % 2 == 1 ) {
count++;
}
if ((g / i) % 2 == 1 ) {
count++;
}
}
else if (i % 2 == 1 ) {
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int G = 125 ;
findValuesOfK(G);
}
}
|
Python3
from math import sqrt
def findValuesOfK(g):
count = 0
for i in range ( 1 , int (sqrt(g)) + 1 , 1 ):
if (g % i = = 0 ):
if (i ! = g / / i):
if (i & 1 ):
count + = 1
if ((g / / i) & 1 ):
count + = 1
elif (i & 1 ):
count + = 1
print (count)
if __name__ = = '__main__' :
G = 125
findValuesOfK(G)
|
C#
using System;
class GFG{
static void findValuesOfK( int g)
{
int count = 0;
for ( int i = 1; i * i <= g; i++)
{
if (g % i == 0)
{
if (i != g / i)
{
if (i % 2 == 1)
{
count++;
}
if ((g / i) % 2 == 1)
{
count++;
}
}
else if (i % 2 == 1)
{
count++;
}
}
}
Console.WriteLine(count);
}
public static void Main()
{
int G = 125;
findValuesOfK(G);
}
}
|
Javascript
<script>
function findValuesOfK(g)
{
var count = 0;
for ( var i = 1; i * i <= g; i++) {
if (g % i == 0) {
if (i != g / i) {
if (i % 2 == 1) {
count++;
}
if ((g / i) % 2 == 1) {
count++;
}
}
else if (i % 2 == 1) {
count++;
}
}
}
document.write(count);
}
var G = 125;
findValuesOfK(G);
</script>
|
Time Complexity: O(?G)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...