📜  找到范围 [L, R] 使得该范围内的数字之和等于 N

📅  最后修改于: 2022-05-13 01:56:06.807000             🧑  作者: Mango

找到范围 [L, R] 使得该范围内的数字之和等于 N

给定一个整数N (N ≠ 0),任务是找到一个范围[L, R] (−10⁻¹⁸ < L < R < 10¹⁸),使得该范围内所有整数的总和等于N。

例子:

朴素方法:对于 L 的每个值,尝试找到满足条件 L + (L+1) + 的值 R。 . . + (R-1) + R = N,使用嵌套循环。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:由于 L 和 R 是整数并且也可以是负数,因此上述问题可以在 O(1) 中有效地解决。考虑以下观察:

  • 对于 N 是一个正整数,我们可以考虑:
  • 同样,对于 N 为负数,我们可以考虑:

因此,这个问题在单位时间复杂度上的解是:

注意:这是满足问题要求的最长可能范围(即 R – L 具有最高值)。

下面是该方法的实现:

C++
// C++ code to implement above approach
 
#include 
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 
 
// 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


输出
-2 3

时间复杂度: O(1)
辅助空间: O(1)