Count of pairs with sum N from first N natural numbers
Last Updated :
30 Mar, 2023
Given an integer N, the task is to count the number of pairs among the first N natural numbers, with sum equal to N.
Examples:
Input: N = 8
Output: 3
Explanation:
All possible pairs with sum 8 are { (1, 7), (2, 6), (3, 5)}
Input: N = 9
Output: 4
Naive Approach:
The simplest approach to solve the problem is to use Two Pointers. Follow the steps below to solve the problem:
- Set i = 0 and j = N – 1 initially.
- Iterate until i >= j, and for every pair of i, j, check if their sum is equal to N or not. If so, increase the count of pairs.
- Move to the next pair by increasing and decreasing i and j by 1 respectively.
- Finally, print the count of pairs obtained.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int numberOfPairs( int n)
{
int count = 0;
int i = 1, j = n - 1;
while (i < j) {
if (i + j == n) {
count++;
}
i++;
j--;
}
return count;
}
int main()
{
int n = 8;
cout << numberOfPairs(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static int numberOfPairs( int n)
{
int count = 0 ;
int i = 1 , j = n - 1 ;
while (i < j)
{
if (i + j == n)
{
count++;
}
i++;
j--;
}
return count;
}
public static void main (String[] args)
{
int n = 8 ;
System.out.println(numberOfPairs(n));
}
}
|
Python3
def numberOfPairs(n):
count = 0
i = 1
j = n - 1
while (i < j):
if (i + j) = = n:
count + = 1
i + = 1
j - = 1
return count
if __name__ = = '__main__' :
n = 8
print (numberOfPairs(n))
|
C#
using System;
class GFG{
public static int numberOfPairs( int n)
{
int count = 0;
int i = 1, j = n - 1;
while (i < j)
{
if (i + j == n)
{
count++;
}
i++;
j--;
}
return count;
}
public static void Main ( string [] args)
{
int n = 8;
Console.Write(numberOfPairs(n));
}
}
|
Javascript
<script>
function numberOfPairs(n)
{
let count = 0;
let i = 1, j = n - 1;
while (i < j)
{
if (i + j == n)
{
count++;
}
i++;
j--;
}
return count;
}
let n = 8;
document.write(numberOfPairs(n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above approach, we just need to observe if N is even or odd. If N is even, the count of possible pairs is N/2 – 1. Otherwise, it is N/2.
Illustration:
N = 8
All possible pairs are (1, 7), (2, 6) and (3, 5)
Hence, count of possible pairs = 3 = 8/2 – 1
N = 9
All possible pairs are (1, 8), (2, 7), (3, 6) and (4, 5)
Hence, count of possible pairs = 4 = 9/2
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int numberOfPairs( int n)
{
if (n % 2 == 0)
return n / 2 - 1;
else
return n / 2;
}
int main()
{
int n = 8;
cout << numberOfPairs(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static int numberOfPairs( int n)
{
if (n % 2 == 0 )
return n / 2 - 1 ;
else
return n / 2 ;
}
public static void main (String[] args)
{
int n = 8 ;
System.out.println(numberOfPairs(n));
}
}
|
Python3
def numberOfPairs(n):
if (n % 2 = = 0 ):
return n / / 2 - 1 ;
else :
return n / / 2 ;
n = 8 ;
print (numberOfPairs(n));
|
C#
using System;
class GFG{
public static int numberOfPairs( int n)
{
if (n % 2 == 0)
return n / 2 - 1;
else
return n / 2;
}
public static void Main ( string [] args)
{
int n = 8;
Console.Write(numberOfPairs(n));
}
}
|
Javascript
<script>
function numberOfPairs(n)
{
if (n % 2 == 0)
return (n / 2 - 1);
else
return (n / 2);
}
let n = 8;
document.write(numberOfPairs(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...