Question: The minimum integer in an array that is at least startno and does not exist in an unordered integer.

For example, the minimum integer not less than 3 in, is 3.

The following test code shows that the implementation is not correct...

Public static final string timedifferenceformats [] = {"day", "Hour", "Minute", "second "}; public static final string timedifferenceformat = "+ [d days] [K hours] [M minutes] [s seconds]"; public static stringbuilder formattimedifference (string fmts [], long differenceinmillis) {stringbuilder sb = new stringbuilder (); long seconds = differenceinmillis/1000; If (differenceinmillis> 60*1000) {long minutes = differenceinmillis/(6 0*1000); If (differenceinmillis> 60*60*1000) {long hours = differenceinmillis/(60*60*1000 ); if (differenceinmillis> 24*60*60*1000) {long days = differenceinmillis/(24*60*60*1000); sb. append (days ). append ("day");} sb. append (hours % 24 ). append ("Hour");} sb. append (minutes % 60 ). append ("Minute");} return sb. append (seconds % 60 ). append ("seconds");} public static void main (string [] ar GS) {long now = system. currenttimemillis (); system. out. println (now); string [] fmts = {"day", "Hour", "Minute", "second"}; system. out. println (formattimedifference (fmts, 62000); Java. util. calendar calendar = Java. util. calendar. getinstance (); Calendar. settimeinmillis (now); system. out. println (calendar. get (Java. util. calendar. day_of_year); system. out. println (New Java. text. simpledateformat ("d days K hours m minutes s seconds "). format (n EW Java. util. date (now); decimalformat df = new decimalformat (", ###"); system. out. println (DF. format (now); int [] data1 = {8899, 9, 2, 11, 1,100}; // 1, 2, 9, 11,100,889 9 int [] data2 = {1, 3, 4, 9, 6, 2}; // 1, 2, 3, 4, 6, 9 int [] data3 = {3}; // 3 int [] data4 = {3, 6}; // 3, 6 system. out. println ("1. min cave number = "+ findmincavenumber (2, data1, false); // 3 system. out. println ("2. m In Cave number = "+ findmincavenumber (2, data2, false); // 5 system. out. println ("3. min cave number = "+ findmincavenumber (2, data3, false); // 2 system. out. println ("4. min cave number = "+ findmincavenumber (5, data4, true )); // 5}/*** data [leftindex] <= start <data [rightindex] ** @ Param start * Start base value * @ Param data * @ Param leftindex * @ Param rightindex * @ return data [leftindex] + 1 or start */Private Static int findmincavenumberbetween (INT startno, int [] data, int leftindex, int rightindex) {// If (rightindex-leftindex <= 1) {int expectedcavenumber = data [leftindex] + 1; return expectedcavenumber <startno? Startno: expectedcavenumber;} int midindex = (leftindex + rightindex)/2; If (startno <data [midindex] & Data [midindex]-data [leftindex]> midindex-leftindex) {return findmincavenumberbetween (startno, Data, leftindex, midindex);} return findmincavenumberbetween (startno, Data, midindex, rightindex );} /***** @ Param data * @ Param start * Start base value * @ Param isorderly * Is Data ordered? * @ return */priv Ate static int findmincavenumber (INT start, int [] data, Boolean isorderly) {If (data. Length = 0) {return start;} If (! Isorderly) {arrays. sort (data);} int min = data [0], max = data [data. length-1]; If (start <min | Start> MAX) {return start;} else if (START = max | max-min = data. length-1) {return start + 1;} else {// min <= start <Max & max-min> = data. length (data. length must> = 2) return findmincavenumberbetween (START, Data, 0, Data. length-1 );}}

1405643181082

1 minute, 2 seconds

199

199 days, 8 hours, 26 minutes, 21 seconds

1,405,643,181,082

1. Min cave number = 3

2. Min cave number = 5

3. Min cave number = 4

4. Min cave number = 5

By the way, the following error occurs:

Locate the missing integer

Question: array A contains n-1 different integers in [0, n-1]. Only one number in N is not in the given array. Design an algorithm for finding the O (n) time of this number. In addition to array a itself, only the extra space of O (1) can be used.

Another similar question can be found in introduction to algorithms. Question 4-2: the problem is the same as above. But here, a single operation cannot access a complete integer in, because the integer in a is expressed in binary. The only operation we can use is to take the J-bits of a [I], and the time taken by this operation is constant. Requirement: it is proved that if this operation is used only, the missing integer can still be found in O (n) time.

You can come up with the following methods for the first question:

Solution 1: The sum of all integers in the range [0, n-1] is N * (n-1)/2, and all elements in array a are summed and set to S, the missing integer is N * (n-1)/2-s.

Solution 2: exclusive or operation. Or an amazing operation. Set the missing integer to K, and the result of all N numbers in the [0, n-1] range is S (n). The XOR operation satisfies the exchange rate and the combination rate, so S (n) it can be seen as removing the exclusive or result s (n-1) and K of the n-1 number outside K in [0, n-1. That is:**S (n) = S (n-1) ^ K**, We give the two sides of the equation an exclusive or S (n-1) at the same time, and the equation becomes:**S (n-1) ^ s (n) = S (n-1) ^ s (n-1) ^ K = K**. Moreover, it is obvious that S (n-1) is actually the exclusive or of all elements in array. Therefore, solution 2 is: Obtain the variance or result s of All integers in [0, n-1], and then obtain the variance or result t of all elements in array, the missing integer is s XOR or T.

Solution 3: A has n-1 positions. You can use a as a hash. Although the result can be obtained, array a is damaged.

As for the solution to the question 4-2 in the introduction to algorithms, I will answer the question in the competition of algorithm art and informatics. Brief Introduction: the binary sequence of natural numbers indicates that the lowest binary digits of all elements in array a are always 0 and 1, if the number of values 0 and 1 is the same, the minimum binary value of the missing integer is 0. Otherwise, the minimum binary value of the missing integer is 0. For example, if we obtain that the minimum binary bit of the missing integer is 0, it means that the integers whose minimum binary bit is 1 in array a are irrelevant to this question, you only need to find the missing integers in the integers whose percentile is 0. Therefore, the time complexity is T (n) = T (n/2) + N. calculation:

The upper or lower integer of n does not affect the time complexity. T (n) = O (n ).