Count of pairs with sum N from first N natural numbers | 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
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 ) ) ;