Task for Alexey Codechef Solution

Hello Programmers In this post, you will know how to solve the Task for Alexey Codechef Solution. The Problem Code: ALEXTASK

Task for Alexey Codechef Solution
Task for Alexey Codechef Solution

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.


Alexey is trying to develop a program for a very simple microcontroller. It makes readings from various sensors over time, and these readings must happen at specific regular times. Unfortunately, if two of these readings occur at the same time, the microcontroller freezes and must be reset.

There are N different sensors that read data on a regular basis. For each i from 1 to N, the reading from sensor i will occur every Ai milliseconds with the first reading occurring exactly Ai milliseconds after the microcontroller is powered up. Each reading takes precisely one millisecond on Alexey’s microcontroller.

Alexey wants to know when the microcontroller will freeze after he turns it on.


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 contains single integer N denoting the number of sensors.

The second line contains N space-separated integers A1, A2, …, AN denoting frequency of measurements. Namely, sensor i will be read every Ai milliseconds with the first reading occurring Ai milliseconds after the microcontroller is first turned on.


For each test case, output a single line containing the number of milliseconds until the microcontroller freezes.


  • 1 ≤ T ≤ 10
  • 2 ≤ N ≤ 500
  • 1 ≤ Ai ≤ 109


Subtask #1 (10 points) ≤ T ≤ 10,2 ≤ N ≤ 91 ≤ Ai ≤ 500Subtask #2 (20 points) ≤ T ≤ 10,2 ≤ N ≤ 500,1 ≤ Ai ≤ 1000Subtask #3 (70 points) original constraints

Sample Input 1 

2 3 5
1 8 7 11
4 4 5 6

Sample Output 1 



Case 1: in 6 milliseconds, the third reading will be attempted from the 1st sensor and the second reading will be attempted from the 2nd sensor.

Case 2: in 7 milliseconds the seventh reading will be attempted from the 1st sensor and the first reading will be attempted from the 3rd sensor.

Case 3: in 4 milliseconds, the first readings from the first two sensors will be attempted.

Task for Alexey CodeChef Solution in JAVA

import java.util.Scanner;
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[] A = new int[N];
			for (int i = 0; i < A.length; i++) {
				A[i] = sc.nextInt();
	static long solve(int[] A) {
		long result = Long.MAX_VALUE;
		for (int i = 0; i < A.length; i++) {
			for (int j = i + 1; j < A.length; j++) {
				result = Math.min(result, lcm(A[i], A[j]));
		return result;
	static long lcm(int a, int b) {
		return (long) a / gcd(a, b) * b;
	static int gcd(int a, int b) {
		return (b == 0) ? a : gcd(b, a % b);

Disclaimer: The above Problem (Task for Alexey) 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.

Next: Team of Two Codechef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *