Generate an N | Java Program to implement the above approach ; Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
import java . io . * ; class GFG { static void Print ( int N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { System . out . print ( - 1 ) ; return ; } int CurEven = 2 , CurOdd = 1 ; int SumOdd = 0 , SumEven = 0 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { System . out . print ( CurEven + " ▁ " ) ; SumEven += CurEven ; CurEven += 2 ; } for ( int i = 0 ; i < N / 2 - 1 ; i ++ ) { System . out . print ( CurOdd + " ▁ " ) ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; System . out . println ( CurOdd ) ; } public static void main ( String [ ] args ) { int N = 12 ; Print ( N ) ; } }