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