Is it possible for a given number s to simply verify that there is any possible arithmetic progression containing n members and the sum of these n-members leads to s.
where the initial element and the difference AP should not be equal to zero.
eg:
s = 24 and n = 4
yes, possibly where the AP is 3 5 7 9.
Note. I just want to check if this is possible or not. No need to find the actual array. 0 <n <10 ^ 9 and 0 <s <10 ^ 18.
My attempt:
we know that the sum of AP is s = n (first + last) / 2;
therefore first + last = 2 * s / n;
2 * s / n must be an integer.
we also know that last = first + (n-1) diff;
so my expression becomes 2 * first + (n-1) diff = 2 * s / n;
first = (2 * s / n - (n-1) diff) / 2; and it must be an integer for a specific diff value.
this is my approach to this, but its temporal complexity is too great to cover 10 ^ 18.
Please, help.:)
source share