Count of possible arrays from prefix | Java implementation of the above approach ; Function to find power of a number . ; Function to find factorial of a number . ; Function to print no of arrays ; c variable counts the no of pairs ; Map to store the frequency of each element ; Sum of all elements of the array ; Variable to check if it is possible to make any array ; First element of suffix array and the last element of prefix array ; Check if the element exists in the map ; If elements of any pair are equal and their frequency is not divisible by 2 update the isArrayPossible variable to false and break through the loop ; If elements of any pair are not equal and their frequency is not same update the isArrayPossible variable to false and break through the loop ; Check if frequency is greater than zero ; update the count of pairs ; Multiply the answer by 2 ^ ( frequency of pairs ) since the elements of the pair are not the same in this condition ; Divide the answer by the factorial of no of similar pairs ; Make frequency of both these elements 0 ; Update the count of pairs ; Divide the answer by the factorial of no . of similar pairs ; Make frequency of this element 0 ; Check if it is possible to make the array and there are n - 1 pairs whose sum will be equal to s1 ; Driver code ; Function calling
import java . util . * ; class GFG { static int power ( int a , int b ) { int result = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) { result = result * a ; } a = a * a ; b = b / 2 ; } return result ; } static int factorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { fact = fact * i ; } return fact ; } static void findNoOfArrays ( int [ ] a , int n ) { int sum = 0 , s1 , c = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( mp . get ( a [ i ] ) == null ) mp . put ( a [ i ] , 1 ) ; else mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; sum = sum + a [ i ] ; } boolean isArrayPossible = true ; int ans = factorial ( n - 1 ) ; s1 = sum / ( n + 1 ) ; if ( mp . get ( s1 ) >= 2 ) { mp . replace ( s1 , mp . get ( s1 ) - 2 ) ; } else { isArrayPossible = false ; } if ( isArrayPossible ) { for ( Map . Entry < Integer , Integer > m : mp . entrySet ( ) ) { if ( m . getKey ( ) == s1 - m . getKey ( ) ) { if ( mp . get ( m . getKey ( ) ) % 2 != 0 ) { isArrayPossible = false ; break ; } } if ( m . getKey ( ) != s1 - m . getKey ( ) ) { if ( mp . get ( m . getKey ( ) ) != mp . get ( s1 - m . getKey ( ) ) ) { isArrayPossible = false ; break ; } } if ( m . getValue ( ) > 0 ) { if ( m . getKey ( ) != s1 - m . getKey ( ) ) { c = c + m . getValue ( ) ; ans = ans * power ( 2 , m . getValue ( ) ) ; ans = ans / factorial ( m . getValue ( ) ) ; mp . replace ( m . getKey ( ) , 0 ) ; mp . replace ( s1 - m . getKey ( ) , 0 ) ; } if ( m . getKey ( ) == s1 - m . getKey ( ) ) { c = c + m . getValue ( ) / 2 ; ans = ans / factorial ( m . getValue ( ) / 2 ) ; mp . replace ( m . getKey ( ) , 0 ) ; } } } } if ( c < n - 1 && isArrayPossible == false ) { System . out . println ( "0" ) ; } else { System . out . println ( ans ) ; } } public static void main ( String args [ ] ) { int [ ] arr1 = { 5 , 2 , 3 , 5 } ; int n1 = arr1 . length ; findNoOfArrays ( arr1 , n1 / 2 ) ; int [ ] arr2 = { - 1 , - 1 , - 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 } ; int n2 = arr2 . length ; findNoOfArrays ( arr2 , n2 / 2 ) ; } }