给定两个整数L和R ,任务是查找值在[L,R]范围内的唯一三元组的数量,以使任何两个数字的总和等于第三个数字。
例子:
Input: L = 1, R = 3
Output: 3
Explanation: Three such triplets satisfying the necessary conditions are (1, 1, 2), (1, 2, 3) and (2, 1, 3).
Input: L = 2, R = 6
Output: 6
天真的方法:解决问题的最简单方法是生成[L,R]范围内的所有可能的三胞胎,并计算那些从该对中任意两个数之和等于第三数的三胞胎。检查完所有三元组后,打印获得的总数。
时间复杂度: O((R – L) 3 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- 如果给定范围之间的差小于L ,则不存在任何这样的三元组,其两个数之和等于第三个数。
- 如果给定范围之间的差至少为L ,则(R – L)位于L和R的范围内,即{L,(R – L),R} 。可以看出, L和[L,R – L]范围内的任何其他数字的总和最多为R。
- 因此,第一个元素为L的可能有效三元组的总数由(R – L – L + 1)给出。
- 同样,当第一个元素为(L + 1)时,三元组的数量为(R – L – L) ,依此类推。
根据上述观察,三元组的总数构成了一个AP,其术语为(R – L – L + 1),(R – L – L),(R – L – L – 1),………,由( R – L – L +1)项数。因此,三元组的总数由下式给出:
where, N is (R – L – L + 1)
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of triplets
// from the range [L, R] having sum of two
// numbers from the triplet equal to the third number
int totalCombination(int L, int R)
{
// Stores the total number of triplets
int count = 0;
// Find the difference of the range
int K = R - L;
// Case 1: If triplets can't
// be formed, then return 0
if (K < L)
return 0;
// Otherwise
int ans = K - L;
// Update the total number of triplets
count = ((ans + 1) * (ans + 2)) / 2;
// Return the count
return count;
}
// Driver Code
int main()
{
int L = 2, R = 6;
cout << totalCombination(L, R);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the number of triplets
// from the range [L, R] having sum of two
// numbers from the triplet equal to the third number
static int totalCombination(int L, int R)
{
// Stores the total number of triplets
int count = 0;
// Find the difference of the range
int K = R - L;
// Case 1: If triplets can't
// be formed, then return 0
if (K < L)
return 0;
// Otherwise
int ans = K - L;
// Update the total number of triplets
count = ((ans + 1) * (ans + 2)) / 2;
// Return the count
return count;
}
// Driven Code
public static void main(String[] args)
{
int L = 2, R = 6;
System.out.print(totalCombination(L, R));
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach
# Function to find the number of triplets
# from the range [L, R] having sum of two
# numbers from the triplet equal to the third number
def totalCombination(L, R):
# Stores the total number of triplets
count = 0
# Find the difference of the range
K = R - L
# Case 1: If triplets can't
# be formed, then return 0
if (K < L):
return 0
# Otherwise
ans = K - L
# Update the total number of triplets
count = ((ans + 1) * (ans + 2)) // 2
# Return the count
return count
# Driver Code
if __name__ == '__main__':
L, R = 2, 6
print (totalCombination(L, R))
# This code is contributed by mohit kumar 29.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the number of triplets
// from the range [L, R] having sum of two
// numbers from the triplet equal to the third number
static int totalCombination(int L, int R)
{
// Stores the total number of triplets
int count = 0;
// Find the difference of the range
int K = R - L;
// Case 1: If triplets can't
// be formed, then return 0
if (K < L)
return 0;
// Otherwise
int ans = K - L;
// Update the total number of triplets
count = ((ans + 1) * (ans + 2)) / 2;
// Return the count
return count;
}
// Driver Code
public static void Main()
{
int L = 2, R = 6;
Console.WriteLine(totalCombination(L, R));
}
}
// This code is contributed by sauravghosh0416.
Javascript
输出:
6
时间复杂度: O(1)
辅助空间: O(1)