Given a positive integer **N** such that there exists two arrays **a[]** and **b[]** each containing values {1, 2, 3, .., N}, the task is to find the count of all pairs (a[i], b[j]) such that a[i] + b[j] is unique among all the pairs i.e. if two pairs have equal sum then only one will be counted in the result.**Examples:**

Input:N = 2Output:3Explanation:

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 = 3Output: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

**Naive approach:**

To solve the problem mentioned above, the naive approach is to to use to a Set to store distinct sums of {1, 2, 3, .. N} and {1, 2, 3, .. N} by using two loops.

Below is the implementation of above approach:

## C++

`// C++ implementation to count ` `// of distinct pair sum between ` `// two Array with values 1 to N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the distinct sums ` `int` `findDistinctSums(` `int` `n) ` `{ ` ` ` `// Set to store distinct sums ` ` ` `set<` `int` `> 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; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer> 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Efficient approach:** To optimize the above method:

- Observe that the series formed for the count of distinct sums in {1, 2, 3, .., N} and {1, 2, 3, .., N} is given as
**1, 3, 5, 7, …** - Therefore Nth term of the above series =
**2 * N – 1** - Hence the count of distinct pair sum can be calculated as
**2 * N – 1**

Below is the implementation of above approach:

## C++

`// C++ implementation to find count ` `// of distinct pair sum between ` `// two 1 to N value Arrays ` ` ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity: **O(1)**Auxiliary Space Complexity: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Count distinct pairs from two arrays having same sum of digits
- Count of pairs between two arrays such that the sums are distinct
- Count pairs formed by distinct element sub-arrays
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Find a distinct pair (x, y) in given range such that x divides y
- Pair with maximum GCD from two arrays
- Find Sum of pair from two arrays with maximum sum
- Find the closest pair from two sorted arrays
- Permute two arrays such that sum of every pair is greater or equal to K
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Python List Comprehension to find pair with given sum from two arrays
- Find a pair of elements swapping which makes sum of two arrays same
- Smallest Difference pair of values between two unsorted Arrays
- Total distinct pairs of ugly numbers from two arrays
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count distinct Bitwise OR of all Subarrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.