//We provide the solution of the last problem of Friday lab test. //The other two problems are easy and almost all of you solved them. //The third problem is generalization of combinations //We enforce that each element is present at least k times and at most //max times in each combinations. This is how we proceed : //We first select each element k times, and let S be the string //corresponding to it. Now We append it with //all combinations of length (L-A.length*k) where each element appears //at most m times. We now suitably change our method of generating //Combinations with repetitions as given in practise exercises on //recursions. class generic_combination { //This method prints all combinations whose String representation //(WHERE ORDER AMONG CHARACTERS DOES NOT MATTER) consists of S //concatenated with L characters from {A[i],A[i+1],...} //with A[i] appearing at most count times, and A[j], for j>i appearing //at most m times. public static void GenCombS(char[] A, int i, int count, int L, int m, String S) { if(L==0) System.out.println("{"+S+"-}"); else { if(count>0) GenCombS(A,i,count-1,L-1,m,S+"-"+A[i]); if((A.length-i-1)*m>=L) GenCombS(A,i+1,m,L,m,S); } } //This method is used jst for better readability and understandability //Otherwise we could have directly called CombS(A,0,L,"") from main; public static void GenComb(char[] A, int L, int k, int max) { String S = With_least_character_count(A,k); int m = max-k; L = L - (A.length)*k; GenCombS(A,0,m,L,m,S); } public static String With_least_character_count(char[] A, int k) { String S=""; for(int index=0; indexmax || A.length*k>L) System.out.println("Sorry, no such combinationis possible"); else GenComb(A,L,k,max); } }