Count ways to split array into K non | Java program to implement the above approach ; Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } static int cntWays ( int arr [ ] , int N , int K ) { int cntways = 0 ; int M = 0 ; Set < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } M = st . size ( ) ; cntways = power ( K , M ) ; return cntways ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 } ; int N = arr . length ; int K = 2 ; System . out . println ( cntWays ( arr , N , K ) ) ; } }