Print all distinct Coprime sets possible from 1 to N | Java implementation to print all distinct co - prime sets possible for numbers from 1 to N ; Function to print all co - prime sets ; Check if n is less than 4 then simply print all values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; If n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver code
import java . util . * ; class GFG { static void coPrimeSet ( int n ) { int firstadj , secadj ; if ( n < 4 ) { System . out . print ( " ( ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i + " , ▁ " ) ; System . out . print ( ")NEW_LINE"); } else { if ( n % 2 == 0 ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } } else { for ( int i = 0 ; i < n / 2 - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } System . out . print ( " ( " + ( n - 2 ) + " , ▁ " + ( n - 1 ) + " , ▁ " + n + ")NEW_LINE"); } } } public static void main ( String [ ] args ) { int n = 5 ; coPrimeSet ( n ) ; } }