Count ways to distribute exactly one coin to each worker | Java program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function Call
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int solve ( Vector < Integer > values , Vector < Integer > salary ) { int ret = 1 ; int amt = 0 ; Collections . sort ( values ) ; Collections . sort ( salary ) ; while ( salary . size ( ) > 0 ) { while ( values . size ( ) > 0 && values . get ( values . size ( ) - 1 ) >= salary . get ( salary . size ( ) - 1 ) ) { amt ++ ; values . remove ( values . size ( ) - 1 ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . remove ( salary . size ( ) - 1 ) ; } return ret ; } public static void main ( String [ ] args ) { Vector < Integer > values = new Vector < Integer > ( ) ; values . add ( 1 ) ; values . add ( 2 ) ; Vector < Integer > salary = new Vector < Integer > ( ) ; salary . add ( 2 ) ; System . out . print ( solve ( values , salary ) ) ; } }