Hello Programmers In this post, you will know how to solve the Andrew and the Meatballs again Codechef Solution. The Problem Code: AMMEAT2
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Problem
Andrew likes meatballs very much as you know.
He has N plates of meatballs, here the ith plate contains exactly i meatballs. Andrew wants to take exactly K plates to his trip to Las Vegas.
On this occasion, he wants to choose the K plates by a strange way: if both ith and jth plates are chosen, then i and j must not be relative prime, for all 1 ≤ i < j ≤ N.
Please help him to choose K plates. Output one of the possible choices.
Input
The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The first line and only line of each test case contains two space-separated integers N and K.
Output
For each test case, output K distinct integers, denoting the number of selected plates, where the plates are numbered from 1 to N. If there are multiple solutions, any one will do. If it is impossible to choose K plates, print only one integer -1.
Constraints
- 1 ≤ T ≤ 7
- 1 ≤ K ≤ N ≤ 777
Sample Input 1
2
100 3
100 100
Sample Output 1
45 63 35
-1
Andrew and the Meatballs again CodeChef Solution in JAVA
import java.util.Arrays; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tc = 0; tc < T; tc++) { int N = sc.nextInt(); int K = sc.nextInt(); int[] solution = solve(N, K); if (solution == null) { System.out.println(-1); } else { System.out.println(String.join(" ", Arrays.stream(solution).mapToObj(String::valueOf).collect(Collectors.toList()))); } } sc.close(); } static int[] solve(int N, int K) { if (K == 1) { return new int[] { 1 }; } if (K > N / 2) { return null; } return IntStream.rangeClosed(1, K).map(x -> x * 2).toArray(); } }
Disclaimer: The above Problem (Andrew and the Meatballs again) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.