Computer Science, asked by aseemalatheef, 10 months ago

Write a java program to find the maximum difference between any pair ofof numbers.The program should display the first occurrence of index positions of the pair that has the maximum difference.

Answers

Answered by dhruvinkachhadia
0

The code is:

static int maxDifference(int[] a) {

//test array size

if (a.length < 1 || a.length > 1_000_000) return -1;

int[] oldArr = Arrays.copyOf(a, a.length);

Arrays.sort(a);

int max = a[a.length - 1];

if (max > 1_000_000 || a[0] < -1_000_000) return -1;

int min = max;

for (int i = 0; i < oldArr.length; i++) {

if (oldArr[i] < max) {

min = Math.min(min, oldArr[i]);

}

if (oldArr[i] == max) break;

}

int result = min == max ? -1 : max - min;

return result;

}

Kindly mark it as Brainliest if find it useful. Thank you!

Similar questions