Distributing all balls without repetition | Java program to find if its possible to distribute balls without repitiio ; 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
import java . io . * ; public class GFG { static int MAX_CHAR = 26 ; static boolean distributingBalls ( long k , long n , String str ) { int [ ] a = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } static public void main ( String [ ] args ) { long n = 6 , k = 3 ; String str = " aacaab " ; if ( distributingBalls ( k , n , str ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }