Home > Back-end >  2020-10-30: given a positive array arr (i.e., the array elements are all positive, find out the arra
2020-10-30: given a positive array arr (i.e., the array elements are all positive, find out the arra

Time:10-31

The 2020-10-30: given a positive array arr (i.e., the array elements are all positive, find out the array, a maximum of two elements subtraction, one of the minuend subscript subscript is not less than the reduction, namely out: maxValue=https://bbs.csdn.net/topics/max {arr [j] - arr and j>=I} [I]?

CodePudding user response:

 
Public static void main (String [] args) {
Int [] array=,2,5,7,9 {1};
The Integer result=null;
For (int I=array. The length - 1; I> 0; I -) {
For (int j=I; J>=0; J -) {
Int temp=array [I] - array [j];
If (result==null | | resultResult=temp;
}
}
}

System.out.println(result);
}

CodePudding user response:

Two for loop can
Int maxV=Inetger. MIN_VALUE;
for(int i=0; iFor (int j=I + 1; jIf (arr [j] - arr [I] & gt; MaxV) maxV=arr [j] - arr [I];
}
}
System. The out. Println (maxV);

CodePudding user response:

If is I realize: first to sort an array, and then the maxvalue

CodePudding user response:

 
Public static void main (String [] args) {
Int [] arr={12, 2, 23, 54, 65, 21 and 35};
Int maxValue=https://bbs.csdn.net/topics/0;
int i=0;
Int result;
For (int j=1; J & lt; Arr. Length; J++) {
Result=arr [j] - arr [I];
If (result & gt; MaxValue)
MaxValue=https://bbs.csdn.net/topics/result;
Else if (the result & lt; 0)
I=j;
}
System. The out. Println (maxValue);
}
  • Related