📌  相关文章
📜  两个1到N值数组之间的不同对和的计数

📅  最后修改于: 2021-05-17 06:37:44             🧑  作者: Mango

给定一个正整数N ,使得存在两个数组a []b [],每个数组包含值{1、2、3,..,N},则任务是查找所有对的计数(a [i], b [j]),使得a [i] + b [j]在所有对中都是唯一的,即,如果两对具有相等的和,则结果中将只计算一个。
例子:

天真的方法:
为了解决上述问题,幼稚的方法是使用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. 观察到为{1、2、3,..,N}和{1、2、3,..,N}中不同和的计数而形成的级数分别为1、3、5、7,…
  2. 因此,上述系列的第N个项= 2 * N – 1
  3. 因此,不同对和的计数可以计算为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)