给定一个正整数N ,使得存在两个数组a[]和b[],每个数组都包含值 {1, 2, 3, .., N},任务是找到所有对 (a[i], b[j]) 使得 a[i] + b[j] 在所有对中是唯一的,即如果两对的总和相等,那么结果中只会计算一个。
例子:
Input: N = 2
Output: 3
Explanation:
a[] = {1, 2}, b[] = {1, 2}
The three possible pairs are (a[0], b[0]), (a[1], b[0]) and (a[1], b[1]).
Pair 1: 1 + 1 = 2
Pair 2: 2 + 1 = 3
Pair 3: 2 + 2 = 4
Input: N = 3
Output: 5
a[] = {1, 2, 3}, b[] = {1, 2, 3}
The possible pairs with distinct sum are:
Pair 1: 1 + 1 = 2
Pair 2: 2 + 1 = 3
Pair 3: 2 + 2 = 4
Pair 4: 3 + 2 = 5
Pair 5: 3 + 3 = 6
幼稚的方法:
为了解决上面提到的问题,最简单的方法是使用一个 Set 来存储 {1, 2, 3, .. N} 和 {1, 2, 3, .. N} 的不同和,使用两个循环。
下面是上述方法的实现:
C++
// C++ implementation to count
// of distinct pair sum between
// two Array with values 1 to N
#include
using namespace std;
// Function to find the distinct sums
int findDistinctSums(int n)
{
// Set to store distinct sums
set s;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
// Inserting every sum
s.insert(i + j);
}
}
// returning distinct sums
return s.size();
}
// Driver code
int main()
{
int N = 3;
cout << findDistinctSums(N);
return 0;
}
Java
// Java implementation to count
// of distinct pair sum between
// two Array with values 1 to N
import java.util.*;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int n)
{
// Set to store distinct sums
HashSet s = new HashSet<>();
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
// Inserting every sum
s.add(i + j);
}
}
// Returning distinct sums
return s.size();
}
// Driver code
public static void main(String[] args)
{
int N = 3;
System.out.print(findDistinctSums(N));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 implementation to count
# of distinct pair sum between
# two Array with values 1 to N
# Function to find the distinct sums
def findDistinctSums(n):
# Set to store distinct sums
s = set()
for i in range(1, n + 1):
for j in range(i, n + 1):
# Inserting every sum
s.add(i + j)
# Returning distinct sums
return len(s)
# Driver code
N = 3
print(findDistinctSums(N))
# This code is contributed by divyamohan123
C#
// C# implementation to count
// of distinct pair sum between
// two Array with values 1 to N
using System;
using System.Collections.Generic;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int n)
{
// Set to store distinct sums
HashSet s = new HashSet();
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
// Inserting every sum
s.Add(i + j);
}
}
// Returning distinct sums
return s.Count;
}
// Driver code
public static void Main(String[] args)
{
int N = 3;
Console.Write(findDistinctSums(N));
}
}
// This code is contributed by gauravrajput1
Javascript
C++
// C++ implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
#include
using namespace std;
// Function to find the distinct sums
int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
int main()
{
int N = 3;
cout << findDistinctSums(N);
return 0;
}
Java
// Java implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
import java.util.*;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
public static void main(String[] args)
{
int N = 3;
System.out.print(findDistinctSums(N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 implementation to find count
# of distinct pair sum between
# two 1 to N value Arrays
# Function to find the distinct sums
def findDistinctSums(N):
return (2 * N - 1)
# Driver code
N = 3
print(findDistinctSums(N))
# This code is contributed by divyamohan123
C#
// C# implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
using System;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
public static void Main()
{
int N = 3;
Console.Write(findDistinctSums(N));
}
}
// This code is contributed by Code_Mech
Javascript
5
高效方法:优化上述方法:
- 观察到为 {1, 2, 3, .., N} 和 {1, 2, 3, .., N} 中的不同和的计数而形成的系列给出为1, 3, 5, 7, …
- 因此上述系列的第 N 项 = 2 * N – 1
- 因此不同对和的计数可以计算为2 * N – 1
下面是上述方法的实现:
C++
// C++ implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
#include
using namespace std;
// Function to find the distinct sums
int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
int main()
{
int N = 3;
cout << findDistinctSums(N);
return 0;
}
Java
// Java implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
import java.util.*;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
public static void main(String[] args)
{
int N = 3;
System.out.print(findDistinctSums(N));
}
}
// This code is contributed by shivanisinghss2110
蟒蛇3
# Python3 implementation to find count
# of distinct pair sum between
# two 1 to N value Arrays
# Function to find the distinct sums
def findDistinctSums(N):
return (2 * N - 1)
# Driver code
N = 3
print(findDistinctSums(N))
# This code is contributed by divyamohan123
C#
// C# implementation to find count
// of distinct pair sum between
// two 1 to N value Arrays
using System;
class GFG{
// Function to find the distinct sums
static int findDistinctSums(int N)
{
return (2 * N - 1);
}
// Driver code
public static void Main()
{
int N = 3;
Console.Write(findDistinctSums(N));
}
}
// This code is contributed by Code_Mech
Javascript
5
时间复杂度: O(1)
辅助空间复杂度: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live