Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the range such that sum of numbers in this range...

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




// 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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments