找到范围 [L, R] 使得该范围内的数字之和等于 N
给定一个整数N (N ≠ 0),任务是找到一个范围[L, R] (−10⁻¹⁸ < L < R < 10¹⁸),使得该范围内所有整数的总和等于N。
L + (L+1) + … + (R−1) + R = N
例子:
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
朴素方法:对于 L 的每个值,尝试找到满足条件 L + (L+1) + 的值 R。 . . + (R-1) + R = N,使用嵌套循环。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:由于 L 和 R 是整数并且也可以是负数,因此上述问题可以在 O(1) 中有效地解决。考虑以下观察:
- 对于 N 是一个正整数,我们可以考虑:
[−(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
- 同样,对于 N 为负数,我们可以考虑:
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)
因此,这个问题在单位时间复杂度上的解是:
L = -(N – 1) and R = N, when N is a positive integer.
L = N and R = -(N + 1), when N is a negative integer.
注意:这是满足问题要求的最长可能范围(即 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)