Open In App

Find the range [L, R] such that sum of numbers in this range equals to N

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find two integers
void Find_Two_Intergers(long long int N)
{
    // Variable to store value of L and R
    long long int L, R;
 
    // When N is positive
    if (N > 0) {
        L = -(N - 1);
        R = N;
    }
 
    // When N is negative
    else {
        L = N;
        R = -(N + 1);
    }
    cout << L << " " << R;
}
 
// Driver Code
int main()
{
    long long int N = 3;
    Find_Two_Integers(N);
    return 0;
}


C




// C code to implement above approach
 
#include <stdio.h>
 
// Function to find two integers
void Find_Two_Intergers(long long int N)
{
    // Variable to store L and R
    long long int L, R;
 
    // When N is positive
    if (N > 0) {
        L = -(N - 1);
        R = N;
    }
 
    // When N is negative
    else {
        L = N;
        R = -(N + 1);
    }
    printf("%lld %lld", L, R);
}
 
// Driver code
int main()
{
    long long int N = 3;
    Find_Two_Integers(N);
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to find two integers
  static void Find_Two_Intergers(long  N)
  {
     
    // Variable to store value of L and R
    long  L, R;
 
    // When N is positive
    if (N > 0) {
      L = -(N - 1);
      R = N;
    }
 
    // When N is negative
    else {
      L = N;
      R = -(N + 1);
    }
    System.out.print( L + " " + R);
  }
 
  // Driver Code
  public static void main (String[] args) {
 
    long N = 3;
    Find_Two_Integers(N);
 
  }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python code to implement above approach
 
# Function to find two integers
def Find_Two_Intergers(N):
    # variable to store L and R
    L = 0
    R = 0
 
    # When N is positive
    if N > 0:
        L = -(N-1)
        R = N
 
    # When N is negative
    else:
        L = N
        R = -(N+1)
 
    print(L, R)
 
 
# Driver code
N = 3
Find_Two_Integers(N)


C#




// C# code for the above approach
using System;
 
class GFG
{
   
  // Function to find two integers
  static void Find_Two_Intergers(long  N)
  {
     
    // Variable to store value of L and R
    long  L, R;
 
    // When N is positive
    if (N > 0) {
      L = -(N - 1);
      R = N;
    }
 
    // When N is negative
    else {
      L = N;
      R = -(N + 1);
    }
    Console.Write( L + " " + R);
  }
 
  // Driver Code
  public static void Main (String[] args) {
 
    long N = 3;
    Find_Two_Integers(N);
 
  }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript




<script>
// Javascript code to implement above approach
 
// Function to find two integers
function Find_Two_Intergers(N) {
    // Variable to store value of L and R
    let L, R;
 
    // When N is positive
    if (N > 0) {
        L = -(N - 1);
        R = N;
    }
 
    // When N is negative
    else {
        L = N;
        R = -(N + 1);
    }
    document.write(L + " " + R);
}
 
// Driver Code
 
let N = 3;
Find_Two_Integers(N);
 
// This code is contributed by gfgking.
</script>


Output

-2 3

Time Complexity: O(1)
Auxiliary Space: O(1)



Last Updated : 28 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads