Problem Description:
Find the default number between the maximum and minimum values in an array, such as int arrdemo = {1, 3, 7}; Then it is necessary to output the missing number between the minimum value of 1 and the maximum value of 7 2,4,5,6
Code below, there is a better way to welcome everyone in the comment area message discussion
1 Packagetest;2 3 Public classTest {4 5 Static int[] Array = {6-10,0,3,3,97 };8 9 Private Static voidPrintemptyitems (int[] Array) {Ten if(Array = =NULL) { One return; A } - - intMin = array[0]; the intmax = Array[0]; - for(inti = 0; I <array.length; ++i) { - if(Array[i] >max) { -Max =Array[i]; + } - if(Array[i] <min) { +Min =Array[i]; A } at } - - intNewlength = max-min; -System.out.println ("min:" + min + "Max:" + Max + "New length is:" +newlength); - int[]newarray =New int[newlength]; - for(inti = 0; i < newlength; i++) { inNewarray[i] = min +i; - } to + for(inti = 0; i < Array.Length; ++i) { - if(Array[i] >= min && Array[i] <max) { theNewarray[array[i]-min] = max + 1; * } $ }Panax Notoginseng - for(inti = 0; i < newlength; i++) { the if(Newarray[i]! = max + 1) { +System.out.println ("Empty item is:" +newarray[i]); A } the } + } - $ Public Static voidMain (string[] args) { $ //TODO auto-generated Method Stub - Printemptyitems (array); - } the -}
Java implementation algorithm for finding the default number of the maximum and minimum values in an array