Distributing all balls without repetition | Javascript program to find if its possible to distribute balls without repitiion ; function to find if its possible to distribute balls or not ; count array to count how many times each color has occurred ; increasing count of each color every time it appears ; to check if any color appears more than K times if it does we will print NO ; Driver code
let MAX_CHAR = 26 ; function distributingBalls ( k , n , str ) { let a = new Array ( MAX_CHAR ) ; a . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } let n = 6 , k = 3 ; let str = " " ; if ( distributingBalls ( k , n , str ) ) document . write ( " " ) ; else document . write ( " " ) ;