2016 Mushroom Street Programming question: Maximum interval

Source: Internet
Author: User

Title Descriptiongiven an ascending sequence, A1 <a2 <...<an. The maximum interval for defining this sequence is D=max{ai+1-ai} (1≤i<n), which is now to be from A2,A3. Removes an element from the an-1. What is the minimum interval for the remaining sequence ?
Input Description:
The first line, a positive integer n (1<=n<=100), the sequence length; next n a positive integer less than 1000, representing an ascending sequence.
Output Description:
Output the answer.
Input Example:
5
1 2 3) 7 8
Output Example:
4
Solving

When deleting an element at the maximum interval of two elements in the original array, the maximum interval increases, and when in other locations, the maximum interval is constant, and the interval between the other locations increases, but does not increase beyond the maximum interval of the original array
The minimum value for all deletions of one element is the maximum interval of the original array
ImportJava.util.Scanner; Public classmain{ Public Static voidmain (String [] args) {Scanner in=NewScanner (system.in); intN,maxd=0; int[] A;  while(In.hasnext ()) {n=In.nextint (); A=New int[n];  for(intI =0;i<n;i++) A[i]=In.nextint (); Maxd=maxdistancemin (a,n);        System.out.println (Maxd); }    }     Public Static intMaxdistancemin (int[] A,intN) {        intMaxd =-1;  for(inti=1;i<= n-1;i++) {Maxd= Math.max (Maxd,a[i]-a[i-1]); }        returnMaxd; }}
Is that an opportunistic thing?
Delete Two elements of a non-element maximum interval, possibly forming the maximum interval
Such as:
1 3 5 8 11 12 Maximum interval: 3
Delete 3,1 5 8 11 12 Maximum interval: 4
Delete 5,1 3 8 11 12 Maximum interval: 5
Delete 8,1 3 5 11 12 Maximum interval: 6
Delete 11,1 3 5 8 12 Maximum interval: 4

The maximum interval minimum is 4, the above program is a problem, the test sample is not considered for special cases
ImportJava.util.Scanner; Public classmain{ Public Static voidmain (String [] args) {Scanner in=NewScanner (system.in); intN,maxd=0; int[] A;  while(In.hasnext ()) {n=In.nextint (); A=New int[n];  for(intI =0;i<n;i++) A[i]=In.nextint (); Maxd=maxdistancemin (a,n);        System.out.println (Maxd); }    }     Public Static intMaxdistancemin (int[] A,intN) {        intMaxd =-1; intMinmaxd =Integer.max_value; int[] D =New int[N-1]; //record the interval of two adjacent elements         for(inti=1;i<= n-1;i++) {D[i-1] = a[i]-a[i-1]; Maxd= Math.max (maxd,d[i-1]);//find the maximum interval        }        //interval D vector element merging, when the minimum value is selected when greater than Maxd         for(inti=0;i<d.length-1; i++){            intSUBD = D[i] + d[i+1]; if(subd> Maxd) {//the new maximum intervalMinmaxd =math.min (MINMAXD,SUBD); }Else{Minmaxd=math.min (Minmaxd,maxd); }        }        returnMinmaxd; }}






2016 Mushroom Street Programming question: Maximum interval

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.