Count even length binary sequences with same sum of first and second half bits | A Naive Recursive Java program to count even length binary sequences such that the sum of first and second half bits is same ; diff is difference between sums first n bits and last n bits respectively ; We can 't cover difference of more  than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Driver program
import java . io . * ; class GFG { static int countSeq ( int n , int diff ) { if ( Math . abs ( diff ) > n ) return 0 ; if ( n == 1 && diff == 0 ) return 2 ; if ( n == 1 && Math . abs ( diff ) == 1 ) return 1 ; int res = countSeq ( n - 1 , diff + 1 ) + 2 * countSeq ( n - 1 , diff ) + countSeq ( n - 1 , diff - 1 ) ; return res ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( " Count ▁ of ▁ sequences ▁ is ▁ " + countSeq ( 2 , 0 ) ) ; } }