Knowee
Questions
Features
Study Tools

You are given an array 𝐴A of length 𝑁N, and an integer 𝐾K.You can perform the following operation:Choose any index 𝑖i (1≤𝑖≤𝑁1≤i≤N), and increase 𝐴𝑖A i​ by 𝐾K.Find the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) attainable, if you can perform this operation as many times as you like (possibly, zero times).Input FormatThe 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 𝐾K — the length of the array and the parameter 𝐾K.The second line contains 𝑁N space-separated integers 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ — the initial values of the array elements.Output FormatFor each test case, output on a new line the answer: the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) if you can perform the given operation any number of times.Constraints1≤𝑇≤1051≤T≤10 5 1≤𝑁≤2⋅1051≤N≤2⋅10 5 1≤𝐾≤1091≤K≤10 9 1≤𝐴𝑖≤1091≤A i​ ≤10 9 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:InputOutput43 41 5 43 212 8 44 11 43 62 8256 121 2 4 128 130 1311008Explanation:Test case 11: Increase the first element by 𝐾=4K=4 to obtain the array [5,5,4][5,5,4].Here, max⁡−min⁡=5−4=1max−min=5−4=1, which is the best possible.Test case 22: The second and third elements can be increased by 22 till they reach 1212, at which point all the elements of the array are equal, so max⁡(𝐴)−min⁡(𝐴)=0max(A)−min(A)=0.Test case 33: Since 𝐾=1K=1, again it's possible to make all the elements equal.Test case 44: Do the following:Increase 𝐴1A 1​ by 1212 repeatedly to make it 133133.Increase 𝐴2A 2​ by 1212 repeatedly to make it 134134.Increase 𝐴3A 3​ by 1212 repeatedly to make it 136136.The array is now [133,134,136,128,130,131][133,134,136,128,130,131].For this array, max⁡(𝐴)−min⁡(𝐴)=136−128=8max(A)−min(A)=136−128=8.It can be shown that this is optimal.

Question

You are given an array 𝐴A of length 𝑁N, and an integer 𝐾K.You can perform the following operation:Choose any index 𝑖i (1≤𝑖≤𝑁1≤i≤N), and increase 𝐴𝑖A i​ by 𝐾K.Find the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) attainable, if you can perform this operation as many times as you like (possibly, zero times).Input FormatThe 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 𝐾K — the length of the array and the parameter 𝐾K.The second line contains 𝑁N space-separated integers 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ — the initial values of the array elements.Output FormatFor each test case, output on a new line the answer: the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) if you can perform the given operation any number of times.Constraints1≤𝑇≤1051≤T≤10 5 1≤𝑁≤2⋅1051≤N≤2⋅10 5 1≤𝐾≤1091≤K≤10 9 1≤𝐴𝑖≤1091≤A i​ ≤10 9 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:InputOutput43 41 5 43 212 8 44 11 43 62 8256 121 2 4 128 130 1311008Explanation:Test case 11: Increase the first element by 𝐾=4K=4 to obtain the array [5,5,4][5,5,4].Here, max⁡−min⁡=5−4=1max−min=5−4=1, which is the best possible.Test case 22: The second and third elements can be increased by 22 till they reach 1212, at which point all the elements of the array are equal, so max⁡(𝐴)−min⁡(𝐴)=0max(A)−min(A)=0.Test case 33: Since 𝐾=1K=1, again it's possible to make all the elements equal.Test case 44: Do the following:Increase 𝐴1A 1​ by 1212 repeatedly to make it 133133.Increase 𝐴2A 2​ by 1212 repeatedly to make it 134134.Increase 𝐴3A 3​ by 1212 repeatedly to make it 136136.The array is now [133,134,136,128,130,131][133,134,136,128,130,131].For this array, max⁡(𝐴)−min⁡(𝐴)=136−128=8max(A)−min(A)=136−128=8.It can be shown that this is optimal.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to find the minimum possible difference between the maximum and minimum elements in an array. You can increase any element in the array by a given number K as many times as you want.

Here are the steps to solve this problem:

  1. First, sort the array in ascending order. This will allow us to easily access the smallest and largest elements.

  2. Then, check if the difference between the second smallest element and the smallest element is greater than K. If it is, add K to the smallest element.

  3. Repeat this process for the second largest and largest elements. If the difference between them is greater than K, subtract K from the largest element.

  4. Continue this process until the difference between the second smallest and smallest elements, and the second largest and largest elements, is less than or equal to K.

  5. Finally, return the difference between the largest and smallest elements in the array.

This algorithm ensures that the difference between the largest and smallest elements is minimized, as we are always trying to bring the smallest and largest elements closer to the middle of the array.

This problem has been solved

Similar Questions

K-ClosenessYou are given an array 𝐴A of length 𝑁N, and an integer 𝐾K.You can perform the following operation:Choose any index 𝑖i (1≤𝑖≤𝑁1≤i≤N), and increase 𝐴𝑖A i​ by 𝐾K.Find the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) attainable, if you can perform this operation as many times as you like (possibly, zero times).Input FormatThe 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 𝐾K — the length of the array and the parameter 𝐾K.The second line contains 𝑁N space-separated integers 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ — the initial values of the array elements.Output FormatFor each test case, output on a new line the answer: the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) if you can perform the given operation any number of times.Constraints1≤𝑇≤1051≤T≤10 5 1≤𝑁≤2⋅1051≤N≤2⋅10 5 1≤𝐾≤1091≤K≤10 9 1≤𝐴𝑖≤1091≤A i​ ≤10 9 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:InputOutput43 41 5 43 212 8 44 11 43 62 8256 121 2 4 128 130 1311008Explanation:Test case 11: Increase the first element by 𝐾=4K=4 to obtain the array [5,5,4][5,5,4].Here, max⁡−min⁡=5−4=1max−min=5−4=1, which is the best possible.Test case 22: The second and third elements can be increased by 22 till they reach 1212, at which point all the elements of the array are equal, so max⁡(𝐴)−min⁡(𝐴)=0max(A)−min(A)=0.Test case 33: Since 𝐾=1K=1, again it's possible to make all the elements equal.Test case 44: Do the following:Increase 𝐴1A 1​ by 1212 repeatedly to make it 133133.Increase 𝐴2A 2​ by 1212 repeatedly to make it 134134.Increase 𝐴3A 3​ by 1212 repeatedly to make it 136136.The array is now [133,134,136,128,130,131][133,134,136,128,130,131].For this array, max⁡(𝐴)−min⁡(𝐴)=136−128=8max(A)−min(A)=136−128=8.It can be shown that this is optimal.

You are given an array 𝐴A of size 𝑁N.Find the largest integer 𝐾K such that there exists a subsequence 𝑆S of length 𝐾K where 𝐾K is divisible by the number of distinct elements in 𝑆S.Input FormatThe first line contains a single integer 𝑇T, denoting the number of test cases.The first line of each test case contains a positive integer 𝑁N, the length of array 𝐴A.The second line contains 𝑁N space-separated integers, 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ −− denoting the array 𝐴A.Output FormatFor each test case, output the largest valid 𝐾K.Constraints1≤𝑇≤1041≤T≤10 4 1≤𝐴𝑖≤𝑁≤2⋅1051≤A i​ ≤N≤2⋅10 5 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:

You are given an array 𝐴A of length 𝑁N, and a positive integer 𝐾K.It is guaranteed that 1≤𝐴𝑖≤𝐾1≤A i​ ≤K for every index 𝑖i from 11 to 𝑁N.You can do the following at most once:Choose an index 𝑖i (1≤𝑖≤𝑁1≤i≤N) and a value 𝑥x (1≤𝑥≤𝐾1≤x≤K).Then, set 𝐴𝑖:=𝑥A i​ :=x.Find the maximum possible value of the sum of adjacent differences of 𝐴A after performing this operation at most once.That is, maximize the quantity∑𝑖=1𝑁−1∣𝐴𝑖−𝐴𝑖+1∣i=1∑N−1​ ∣A i​ −A i+1​ ∣Input FormatThe 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 𝐾K — the length of the array and the maximum allowed integer 𝐾K, respectively.The second line contains 𝑁N space-separated integers 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ , the elements of array 𝐴A.Output FormatFor each test case, output on a new line the answer: the maximum possible sum of adjacent differences of 𝐴A after replacing exactly one element.Constraints1≤𝑇≤1001≤T≤1001≤𝑁≤10001≤N≤10001≤𝐾≤2⋅1061≤K≤2⋅10 6 1≤𝐴𝑖≤𝐾1≤A i​ ≤KThe sum of 𝑁N across all tests won't exceed 10001000.Sample 1:InputOutput32 51 53 87 2 75 2018 3 1 4 1941263Explanation:Test case 11: It's best to leave the array unchanged, giving us a difference of ∣1−5∣=4∣1−5∣=4.Test case 22: It's optimal to set 𝐴2:=1A 2​ :=1, giving us the array [7,1,7][7,1,7]. The sum of adjacent differences is 6+6=126+6=12.Test case 33: It's optimal to set 𝐴3:=20A 3​ :=20, to obtain [18,3,20,4,19][18,3,20,4,19]. The sum of adjacent differences is 6363.

Array-DifferenceMark has an array of 𝑁 elements of positive integers. He is interested in finding out the maximum absolute difference between any 2 elements in the array. But he wants to minimize this maximum absolute difference.             He can perform the following two types of operation on the array elements any number of times.              If the element 𝐸 is even then he can replace it by 𝐸/2.             If the element 𝐸 is odd then he can replace it by 2𝐸.              Can you help him minimize this maximum absolute difference? Input Format:The first line consists of a single integer 𝑇, the number of test cases.Each test case consists of 2 lines, 1st line has an integer 𝑁, the number of elements in the array.Next lines have 𝑁 space separated integers, denoting array elements.Output Format:For each test case, output in a separate line, the answer to the given question.Constraints:1≤𝑇≤5002≤𝑁≤500001≤𝐴[𝑖]≤106 for each valid 𝑖It is guaranteed that summation of 𝑁 over all test cases doesn't exceed 250000.Sample input121 2Sample output0ExplanationHe can make 1 as 1*2 = 2So array becomes { 2 , 2}Min(max absolute diff) he can get is (2-2) = 0

You are given an array 𝐴A of size 𝑁N.You can rearrange the elements of 𝐴A as you want. After that, construct an array 𝐵B of size 𝑁N as:𝐵1=𝐴1B 1​ =A 1​ ;𝐵𝑖=𝐵𝑖−1⊕𝐴𝑖B i​ =B i−1​ ⊕A i​ for all 2≤𝑖≤𝑁2≤i≤N, where ⊕⊕ denotes the bitwise XOR operator.Find a rearrangement of 𝐴A such that 𝐵𝑖≠0B i​ =0 for all 1≤𝑖≤𝑁1≤i≤N.Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains 𝑁N, the size of the array 𝐴A.The second line of each test case contains 𝑁N space-separated integers : 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ .Output FormatFor each test case, output on a new line, 𝑁N space-separated integers denoting the rearrangement of 𝐴A satisfying the conditions.In case no such rearrangement exists, print −1−1 instead.Constraints1≤𝑇≤2⋅1041≤T≤2⋅10 4 1≤𝑁≤2⋅1051≤N≤2⋅10 5 1≤𝐴𝑖≤1091≤A i​ ≤10 9 The sum of 𝑁N over all test cases does not exceed 2⋅1052⋅10 5 .Sample 1:InputOutput431 2 351 2 3 4 531 2 211-11 2 4 3 51 2 21Explanation:Test case 11 : It can be shown that it's impossible to rearrange 𝐴A in a valid way.Test case 22 : A possible rearrangement of array 𝐴A is [1,2,4,3,5][1,2,4,3,5] and the corresponding array 𝐵B is [1,3,7,4,1][1,3,7,4,1]. Here 𝐵𝑖≠0B i​ =0 for all 1≤𝑖≤𝑁1≤i≤N.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.