coffee break
BAN USERprivate static void MaxDifference(int[] a) {
if(a.length == 0){
System.out.println("Empty Array");
}
int i = 0, minIndex = 0;
int j = a.length - 1, maxIndex = a.length - 1;
int maxDiff = a[j] - a[i];
int count = 0;
while(count < a.length - 1){
if(a[maxIndex] - a[i+1] > maxDiff && a[i+1] < a[minIndex] && maxIndex > i+1){
minIndex = i+1;
maxDiff = a[maxIndex] - a[minIndex];
}
if(a[j - 1] - a[minIndex] > maxDiff && a[j-1] > a[maxIndex] && minIndex < j-1){
maxIndex = j-1;
maxDiff = a[maxIndex] - a[minIndex];
}
i++;j--;count++;
System.out.println("count :" + (count) + " difference : " + maxDiff + " min value " + a[minIndex] + " max value " + a[maxIndex]);
}
System.out.println("difference : " + maxDiff + " min value " + a[minIndex] + " max value " + a[maxIndex]);
}
- coffee break April 10, 2013