Count unimodal and non | Java program for the above approach ; Function to calculate the factorials up to a number ; Calculate the factorial ; Function to find power ( a , b ) ; Iterate until b exists ; If b is divisible by 2 ; Decrease the value of b ; Return the answer ; Function that counts the unimodal and non - unimodal permutations of a given integer N ; Function Call for finding factorials up to N ; Function to count unimodal permutations ; Non - unimodal permutation is N ! - unimodal permutations ; Driver Code ; Given Number N ; Function Call
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int mx = ( int ) 1e6 ; static int [ ] fact = new int [ ( int ) mx + 1 ] ; static void Calculate_factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i <= mx ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; fact [ i ] %= mod ; } } static int UniModal_per ( int a , int b ) { int res = 1 ; while ( b > 0 ) { if ( b % 2 != 0 ) res = res * a ; res %= mod ; a = a * a ; a %= mod ; b /= 2 ; } return res ; } static void countPermutations ( int n ) { Calculate_factorial ( ) ; int uni_modal = UniModal_per ( 2 , n - 1 ) ; int nonuni_modal = fact [ n ] - uni_modal ; System . out . print ( uni_modal + " ▁ " + nonuni_modal ) ; return ; } public static void main ( String [ ] args ) { int N = 4 ; countPermutations ( N ) ; } }