GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Wednesday, 14 September 2022

[Solution] Far Away CodeChef Solution | Solution CodeChef



Problem

Chef has an array A of size N and an integer M, such that 1 \leq A_i \leq M for every 1 \leq i \leq N.

The distance of an array B from array A is defined as:

d(A, B) = \sum_{i=1}^N |A_i - B_i|

Chef wants an array B of size N, such that 1 \le B_i \le M and the value d(A, B) is as large as possible, i.e, the distance of B from A is maximum.

Find the maximum distance for any valid array B.

Note: |X| denotes the absolute value of an integer X. For example, |-4| = 4 and |7| = 7.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two lines of input.
    • The first line of each test case contains two space-separated integers N and M — the length of array A and the limit on the elements of A and B.
    • The second line contains N space-separated integers A_1, A_2, \ldots, A_N.

Output Format

For each test case, output on a new line the maximum distance of an array from A.


Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

Explanation:

Test case 1: The array having maximum distance from A is B = [6, 1]. Thus the distance is |3-6| + |5-1| = 3+4=7.

Test case 2: The only array possible is B = [1,1,1,1]. The distance of this array from A is 0.

Test case 3: One of the possible arrays having maximum distance from A is B = [7,7,1,1,1]. Thus the distance is |2-7| + |3-7| + |4-1| + |5-1| + |6-1| = 5+4+3+4+5=21.

No comments:

Post a Comment