Find the range [L, R] such that sum of numbers in this range equals to N
Given an integer N (N ≠ 0), the task is to find a range [L, R] (−10⁻¹⁸ < L < R < 10¹⁸) such that sum of all integers in this range is equal to N.
L + (L+1) + … + (R−1) + R = N
Examples:
Input : N = 3
Output: -2 3
Explanation: For L = -2 and R = -3 the sum becomes -2 + (-1) + 0 + 1 + 2 + 3 = 3
Input : N = -6
Output: -6 5
Explanation: The sum for this range [-6, 5] is -6 + (-5) + (-4) + (-3) + (-2) + (-1) + 0 + 1+ 2 + 3 + 4 + 5 = -6
Naive Approach: For every value of L try to find a value R which satisfies the condition L + (L+1) + . . . + (R-1) + R = N, using nested loop.
Time Complexity: O(N2)
Auxiliary space: O(1)
Efficient Approach: Since L and R are integers and can be negative numbers as well, the above problem can be solved in O(1) efficiently. Consider the below observation:
- For N being a positive integer we can consider:
[−(N – 1)] + [−(N – 2)] + . . . -1 + 0 + 1 + . . . + (N − 1) + N =
-(N – 1) + (N – 1) – (N – 2) + (N – 2) + . . . + 1 – 1 + 0 + N = N
So, L = -(N – 1) and R = N
- Similarly for N being a negative, we can consider:
N + (N + 1) + . . . -1 + 0 + 1 + . . . + [-(N + 2)] + [-(N + 1)] =
(N + 1) – (N + 1) + (N + 2) – (N + 2) + . . . -1 + 1 + 0 + N = N
So L = N and R = -(N + 1)
Therefore, the solution to this problem in unit time complexity is:
L = -(N – 1) and R = N, when N is a positive integer.
L = N and R = -(N + 1), when N is a negative integer.
Note: This is the longest possible range, (i.e. R – L has the highest value) which satisfies the problem requirement.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Find_Two_Intergers( long long int N)
{
long long int L, R;
if (N > 0) {
L = -(N - 1);
R = N;
}
else {
L = N;
R = -(N + 1);
}
cout << L << " " << R;
}
int main()
{
long long int N = 3;
Find_Two_Integers(N);
return 0;
}
|
C
#include <stdio.h>
void Find_Two_Intergers( long long int N)
{
long long int L, R;
if (N > 0) {
L = -(N - 1);
R = N;
}
else {
L = N;
R = -(N + 1);
}
printf ( "%lld %lld" , L, R);
}
int main()
{
long long int N = 3;
Find_Two_Integers(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void Find_Two_Intergers( long N)
{
long L, R;
if (N > 0 ) {
L = -(N - 1 );
R = N;
}
else {
L = N;
R = -(N + 1 );
}
System.out.print( L + " " + R);
}
public static void main (String[] args) {
long N = 3 ;
Find_Two_Integers(N);
}
}
|
Python3
def Find_Two_Intergers(N):
L = 0
R = 0
if N > 0 :
L = - (N - 1 )
R = N
else :
L = N
R = - (N + 1 )
print (L, R)
N = 3
Find_Two_Integers(N)
|
C#
using System;
class GFG
{
static void Find_Two_Intergers( long N)
{
long L, R;
if (N > 0) {
L = -(N - 1);
R = N;
}
else {
L = N;
R = -(N + 1);
}
Console.Write( L + " " + R);
}
public static void Main (String[] args) {
long N = 3;
Find_Two_Integers(N);
}
}
|
Javascript
<script>
function Find_Two_Intergers(N) {
let L, R;
if (N > 0) {
L = -(N - 1);
R = N;
}
else {
L = N;
R = -(N + 1);
}
document.write(L + " " + R);
}
let N = 3;
Find_Two_Integers(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...