Count of pairs with sum N from first N natural numbers | C ++ Program to implement the above approach ; Function to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pairs is equal to N ; Increase the count of pairs ; Move to the next pair ; Driver Code
#include <iostream> NEW_LINE 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 ; }