[Hackerrank] Sherlock and minimax

Source: Internet
Author: User

Question connection: Sherlock and minimax

Watson gives Sherlock an arrayA1, a2....
He asks him to find an integerMBetweenPAndQ(BOTH aggressive), such that,Min {| ai-M |, 1 ≤ I ≤ n}Is maximized. If there are multiple solutions, print the smallest one.

Input Format 
The first line containsN. The next line contains space separatedNIntegers, and denote the ArrayA. The third line contains two space separated integers denotingPAndQ.

Output Format 
In one line, print the required answer.

Constraints 
1 ≤ n ≤102
1 ≤ AI ≤ 109
1 ≤ p ≤ q ≤ 109

Sample Input

35 8 144 9

Sample output

4

Question: The difficulty is a diffcult question. It's really hard. =

First, the question is understood: given an array A and two integers p and q, find a number m in the interval [p, q], so that min {A [I]-m} is the largest, that is, for the number m between each [p, q], calculate the absolute values of each number and m in the array, then there is a minimum value in these absolute values (such as the example in the question, if M = 4 is selected, the absolute values of the three difference are, 10, and the minimum value is 1 ). Now we need to select this m to maximize the minimum value (for example, if M = 5 is selected, the absolute values of the corresponding three differences are 0, 3, 9, and the minimum value is 0, it is smaller than the minimum value obtained when you select 4, so select 4 for m ).

First, sort the original array. For any m between [p, q], the absolute values of the corresponding difference are shown in two cases:

Figure 1

In the first case, the minimum value reaches at the endpoint; in the second case, the minimum value reaches at the turning point, which is obtained after the array is ordered. So let's think about where the maximum and minimum values can be reached. As shown in the small figure, for any two numbers, if and only when m is their midpoint (A [I] + A [I + 1])/2, the minimum absolute value is the maximum. Therefore, we can traverse all (A [I], a [I + 1]) and find the m that maximizes the minimum value as M.

Figure 2

In this process, the midpoint of (a [I], a [I + 1]) is not in [p, q], so the minimum value is either in [p, q, q] When the intersection between [p, q] and [A [I] And a [I + 1] is obtained at the nearest midpoint, get the minimum value in P or Q); or [p, q] and [A [I], a [I + 1] do not have an intersection, that is, the first case in Figure 1, the minimum value is also reached at P or Q. In this case, we use O (n) time alone to evaluate p and q, traverse the array, find the minimum value, and then check whether it may become the maximum and minimum value.

To sum up the algorithm:

  • First, sort array
  • For p and q, use the time of O (n) to traverse the array and find the minimum value;
  • For any (A [I], a [I + 1]), check whether (a [I] + A [I + 1])/2 is in [p, q]. If the minimum value obtained here can be the maximum and minimum value, note that when a [I] + A [I + 1] is an odd number, we need to check the two midpoint, both of them may obtain the minimum value.
  • The algorithm's sorting time complexity O (nlogn). P, Q, and Midpoint complexity O (n) are processed separately, so the final algorithm's time complexity is O (nlogn ).

The Code is as follows:

 1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4  5  6 public class Solution { 7     static int miniMax = 0; 8     static int miniMax_index = 0; 9     private static int mini_first_last(int[] a,int first_last){10         //check for p11         int mini = Integer.MAX_VALUE;12         13         for(int i = 0;i < a.length;i++){14             mini = Math.min(mini,Math.abs(a[i]-first_last));15         }16         17         return mini;18     }19     private static void middle(int index,int[] a,int p,int q){20         int mini = Integer.MAX_VALUE;21         int mini_index = 0;22         int mid = (a[index]+a[index+1])/2;23         if(mid >= p && mid <= q){24             int m = Math.abs(a[index]-mid);25             int n = Math.abs(a[index+1]-mid);26             if(mini > Math.min(m, n)){27                 mini = Math.min(m, n);28                 mini_index = mid;29             }30             if(mid+1<=q && mid*2 != a[index] + a[index+1]){31                 m = Math.abs(a[index]-mid-1);32                 n = Math.abs(a[index+1]-mid-1);33                 if(mini > Math.min(m, n)){34                     mini = Math.min(m, n);35                     mini_index = mid+1;36                 }37             }38             if(mini != Integer.MAX_VALUE && miniMax < mini){39                 miniMax = mini;40                 miniMax_index = mini_index;41             }42         }    43         44     }45     public static void main(String[] args) {46         Scanner in = new Scanner(System.in);47         int n = in.nextInt();48         int[] a = new int[n];49         for(int i = 0;i < n;i++)50             a[i] = in.nextInt();51         int p = in.nextInt();52         int q = in.nextInt();53         Arrays.sort(a);54         55         miniMax = mini_first_last(a, p);56         miniMax_index = p;57         58         int temp = mini_first_last(a, q);59         if(miniMax < temp){60             miniMax = temp;61             miniMax_index = q;62         }63         64         for(int i = 0;i < n-1;i++){65             middle(i, a, p, q);66         }67         System.out.println(miniMax_index);68       }69 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.