Add general algorithm to allow duplicates upto k times