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
Question
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
Solution
This problem is about finding the minimum maximum absolute difference in an array of integers by performing certain operations. Here are the steps to solve this problem:
- Read the number of test cases, 𝑇.
- For each test case, do the following: a. Read the number of elements in the array, 𝑁. b. Read the 𝑁 elements of the array.
- For each element in the array, if the element is even, replace it with 𝐸/2. If the element is odd, replace it with 2𝐸. Repeat this step until all elements in the array are the same.
- The minimum maximum absolute difference is the difference between the maximum and minimum elements in the array. Since all elements in the array are the same after the operations, the difference is 0.
- Print the minimum maximum absolute difference for each test case.
In the given sample input, there is one test case with an array of two elements: 1 and 2. By replacing 1 with 2𝐸 (where 𝐸 is 1), the array becomes {2, 2}. The minimum maximum absolute difference is 0, which is the output.
Similar Questions
Problem statementTheo, an aspiring mathematician, has presented you with a challenge. He wants you to create a program that calculates the absolute difference between the sum of two arrays.Create a program to find the absolute difference between two arrays with a function named calculateArraySum and calculateAbsoluteDifference where the array is passed as an argument.Input 10-100 -49 -87 5 6 7 0 100 37 5710-100 -75 -48 -86 -98 98 97 98 67 100Output 77ExplanationTwo arrays with 10 elements each are given as input.The first array has elements: -100, -49, -87, 5, 6, 7, 0, 100, 37, 57, and the sum of the first array is - 24.The second array has elements: -100, -75, -48, -86, -98, 98, 97, 98, 67, 100, and the sum of the second array is 53.The absolute difference of the sum is calculated by subtracting the second array from first array |-24 - 53| = 77, and the result is printed.Input format :The first line of input is an integer value 'N1', representing the number of elements in the first array.The second line of input consists of N1 space-separated integers arr1[i], representing the elements of the first array.The third line of input is an integer value 'N2', representing the number of elements in the second array.The fourth line of input consists of N2 space-separated integers arr2[i], representing the elements of the second array.Output format :The output displays a single integer the absolute difference between the sums of the elements in the two arrays.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N1, N2 ≤ 10-100 ≤ arr1[i], arr2[i] ≤ 100Sample test cases :Input 1 :35 8 332 6 4Output 1 :4Input 2 :6-2 5 0 8 -1 364 -3 2 7 1 6Output 2 :4Input 3 :10-100 -49 -87 5 6 7 0 100 37 5710-100 -75 -48 -86 -98 98 97 98 67 100Output 3 :77
Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to .
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.
You are given a sorted array of integers. Write a program that implements a binary search algorithm to find the element with the minimum difference from the given target.Note: This question was asked in CTS coding test.Input format :The first line input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the sorted array elements.The third line consists of an integer representing the target element.Output format :The output prints an integer representing the element with the minimum difference from the given target.
Large DifferencesYou 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.
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.