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 = 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
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++
#include <bits/stdc++.h>
using namespace std;
int findDistinctSums( int n)
{
set< int > s;
for ( int i = 1; i <= n; i++) {
for ( int j = i; j <= n; j++) {
s.insert(i + j);
}
}
return s.size();
}
int main()
{
int N = 3;
cout << findDistinctSums(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findDistinctSums( int n)
{
HashSet<Integer> s = new HashSet<>();
for ( int i = 1 ; i <= n; i++)
{
for ( int j = i; j <= n; j++)
{
s.add(i + j);
}
}
return s.size();
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(findDistinctSums(N));
}
}
|
Python3
def findDistinctSums(n):
s = set ()
for i in range ( 1 , n + 1 ):
for j in range (i, n + 1 ):
s.add(i + j)
return len (s)
N = 3
print (findDistinctSums(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findDistinctSums( int n)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 1; i <= n; i++)
{
for ( int j = i; j <= n; j++)
{
s.Add(i + j);
}
}
return s.Count;
}
public static void Main(String[] args)
{
int N = 3;
Console.Write(findDistinctSums(N));
}
}
|
Javascript
<script>
function findDistinctSums(n)
{
s = new Set();
for ( var i = 1; i <= n; i++) {
for ( var j = i; j <= n; j++) {
s.add(i + j);
}
}
return s.size;
}
var N = 3;
document.write( findDistinctSums(N));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int findDistinctSums( int N)
{
return (2 * N - 1);
}
int main()
{
int N = 3;
cout << findDistinctSums(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findDistinctSums( int N)
{
return ( 2 * N - 1 );
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(findDistinctSums(N));
}
}
|
Python3
def findDistinctSums(N):
return ( 2 * N - 1 )
N = 3
print (findDistinctSums(N))
|
C#
using System;
class GFG{
static int findDistinctSums( int N)
{
return (2 * N - 1);
}
public static void Main()
{
int N = 3;
Console.Write(findDistinctSums(N));
}
}
|
Javascript
<script>
function findDistinctSums(N)
{
return (2 * N - 1);
}
let N = 3;
document.write(findDistinctSums(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space Complexity: O(1)
Please Login to comment...