给定一个正整数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
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
Java脚本
5
时间复杂度: O(1)
辅助空间复杂度: O(1)