The Army Codechef Solution

Hello Programmers In this post, you will know how to solve the The Army Codechef Solution. Problem Code: ANUARM

The Army Codechef Solution
The Army 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.

Problem

N Soldiers are lined up for a memory test. They are numbered from 0 to N-1 from left to right.

In the test, there are M rounds. In each round, Captain selects one position. Soldier at that position will be numbered 0. All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right.
eg. if N = 6 and selected position is 3, then the numbering will be [3, 2, 1, 0, 1, 2].

After M rounds, Captain asked each soldier to shout out the greatest number he was assigned during the M rounds. In order to check the correctness, Captain asked you to produce the correct values for each soldier (That is the correct value each soldier should shout out).

Input

The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains two integers, N and M. Second line of each test case contains M integers, the positions selected by Captain, in that order.

Output

For each test case, output one line with N space separated integers.

Constraints

  • 1 ≤ T ≤ 10^4
  • 1 ≤ N ≤ 10^5
  • 1 ≤ M ≤ 10^5
  • 1 ≤ Sum of N over all testcases ≤ 10^5
  • 1 ≤ Sum of M over all testcases ≤ 10^5
  • 0 ≤ Positions selected by captain ≤ N-1

Example

Sample Input 1 

2
4 1
1
6 2
2 3

Sample Output 1 

1 0 1 2
3 2 1 1 2 3

The Army 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 M = sc.nextInt();
			int[] rounds = new int[M];
			for (int i = 0; i < rounds.length; i++) {
				rounds[i] = sc.nextInt();
			}
			System.out.println(String.join(" ",
					Arrays.stream(solve(N, rounds)).mapToObj(String::valueOf).collect(Collectors.toList())));
		}
		sc.close();
	}
	static int[] solve(int N, int[] rounds) {
		int min = Arrays.stream(rounds).min().getAsInt();
		int max = Arrays.stream(rounds).max().getAsInt();
		return IntStream.range(0, N).map(i -> Math.max(Math.abs(min - i), Math.abs(max - i))).toArray();
	}
}

Disclaimer: The above Problem (The Army) 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: Train Partner Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad