Digs
Digs

Reputation: 193

Maximum difference in an array

For this array {2,6,1,5,10,7} following code will return 10-1=9.Please explain how it works and how did this logic found the minimum value 1 in that array.

void method(int[] ar,int n) {
    int max =ar[1]-ar[0];
    int i,j;
    for( i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++){
            if(ar[j]-ar[i]>max)
                max=ar[j]-ar[i];
        }
    }
    System.out.println(max);
}

Upvotes: 1

Views: 4516

Answers (6)

Mohamed Dernoun
Mohamed Dernoun

Reputation: 825

This solution is used for not sorted array with negative numbers

public int computeDifference() {
    int min_ele = arr[0];
    int max_ele = arr[0];
    
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] < min_ele)
            min_ele = arr[i];
        if (arr[i]>max_ele)
            max_ele = arr[i];
    }
    return Math.abs(max_ele - min_ele);
}

Upvotes: 0

SM ANSARI
SM ANSARI

Reputation: 385

Code complexity is very high in your program.

There is always better way of doing it

mine is

    int[] numArray = {2,6,1,5,10,7};
    int max = numArray[0];
    int min = numArray[0];
    for( int i : numArray){ 
      if( max < i )
        max = i;
      if( min > i )
        min = i;
    }
System.out.println("Maximum Difference of an Array is "+(max - min ));

Upvotes: 1

Raman Sahasi
Raman Sahasi

Reputation: 31841

The reason why it is getting '9' as answer is because the max difference that's being counted is 10-1=9.

Consider the loop when i=2 and j=4

The max difference that's counted is
=> max = ar[j] - ar[i];
=> max = 10 - 1;
=> max = 9


EDIT

But of course, a more optimal approach would be to sort the array and find the difference between first and last element.

Arrays.sort(ar);
System.out.println(ar[ar.length-1] - ar[0]);

Output

9

Upvotes: 1

passion
passion

Reputation: 1360

You just need to find max number and min number within one loop :

int max = Integer.MIN_VALUE , min = Integer.MAX_VALUE;

for(int i = 0 ; i < n ; i ++){
    max = Math.max(arr[i], max);
    min = Math.min(arr[i], min);
}

System.out.println(max - min);

Upvotes: 3

Prashant Negi
Prashant Negi

Reputation: 348

The logic makes an assumption that the maximum difference in the array is between the first two numbers.

Max variable is used to save the maximum difference

max = arr[1] - arr[0] //assumption made that the the first two integers give the max difference

Now we create a nested loop so that difference of a number is calculated with all other values in the array.

But we don't need to check the values that already have been computed to save our time. Since we only need the difference I would like to point out taht we take the difference as always positive. Hence we only take care of the values ahead as

abs(arr[0] - arr[1]) = abs(arr[1] - arr[0])

where abs represents the absolute value.

Now while comparing the difference of the one value with other values whenever the algorithm finds any difference that is greater than the maximum alue it updates the value of max.

So after exiting the loop we have the max difference in the array values.

PS: A better approach would be to sort the array and then subtract the index and the last value

PPS: If you want better understanding of this loop you can also look into Selection Sort

Upvotes: 1

mhasan
mhasan

Reputation: 3709

Try this.

import java.util.Arrays;

    public class Test {
        public static void main(String[] args) {

            int[] a = { 2, 6, 1, 5, 10, 7 };
            Arrays.sort(a);
            System.out.println(a[a.length - 1] - a[0]);
        }
    }

Upvotes: 1

Related Questions