Simultaed all the holidays and then removed them from the list of total days.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P10050 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numTestCases = Integer.parseInt(br.readLine().trim()); for (int testCase = 1; testCase <= numTestCases; testCase++) { int N = Integer.parseInt(br.readLine().trim()); int P = Integer.parseInt(br.readLine().trim()); HashSet<Integer> ans = new HashSet<Integer>(); for (int i = 0; i < P; i++) { int tmp = Integer.parseInt(br.readLine().trim()); int max = N / tmp; for (int j = 1; j <= max; j++) { ans.add(tmp * j); } } int max = N / 7 + 1; for (int j = 1; j <= max; j++) { int tmp = 7 * j; if (ans.contains(tmp)) ans.remove(tmp); if (ans.contains(tmp - 1)) ans.remove(tmp - 1); } out.println(ans.size()); } out.close(); } }