Home > other >  find maximum double in an array in java
find maximum double in an array in java

Time:05-05

Here is the code, I keep not getting the correct output from the getMax method. I fill an array with the sum of the int values of an ArrayList, then check which one of them is the smallest and the biggest, finally outputting them and pruning away the decimal places, which might cause errors maybe? Code might be bad, I'm a noob:

public static void miniMaxSum(ArrayList<Integer> arr) {
    
    double[] acc = new double[arr.size()]; 
    for (int i=0; i<arr.size(); i  ){
        for (int j=0; j<arr.size(); j  ){
            acc[i]  = arr.get(j);
            }
        acc[i] -= arr.get(i);
        }
             
    //double min = acc[arr.size()-1];
    //double max = acc[0];
    System.out.printf("%.0f", getMin(acc));
    System.out.print(" ");
    System.out.printf("%.0f", getMax(acc));     
    }             

public static double getMin(double[] acc){
    double min = 0;
    for (int j=0; j<acc.length; j  ){
        for (int i=0; i<acc.length; i  ){
            if (acc[i] > acc[j]){
                min = acc[j];
            } else {
                min = acc[i];
            }
            
        }
    }     
    return min; 
}
public static double getMax(double[] acc){
    double max = 0;
    for (int j=0; j<acc.length; j  ){
        for (int i=0; i<acc.length; i  ){
            if (acc[i] > acc[j]){
                max = acc[i];
                System.out.println(max);
            } 
            else {
                max = acc[j];
            }
            
        }
    }     
    return max; 
}
       

CodePudding user response:

Method to do the checks yourself

public static double findMax(double[] data) {
    double max = Double.MIN_VALUE;
    for (double val : data) {
        if (val > max) {
            max = val;
        }
    }
    
    return max;
}

Using the Math utility

public static double findMax2(double[] data) {
    double max = Double.MIN_VALUE;
    for (double val : data) {
        max = Math.max(val, max);
    }
    
    return max;
}

Using streams

public static double findMax3(double[] data) {
    return Arrays.stream(data).max().getAsDouble();
}

Doing your own checks

public static double findMin(double[] data) {
    double min = Double.MAX_VALUE;
    for (double val : data) {
        if (val < min) {
            min = val;
        }
    }
    
    return min;
}

Using Math utility

public static double findMin2(double[] data) {
    double min = Double.MAX_VALUE;
    for (double val : data) {
        min = Math.min(val, min);
    }
    
    return min;
}

Using streams

public static double findMin3(double[] data) {
    return Arrays.stream(data).min().getAsDouble();
}

CodePudding user response:

You can do everything you want do do in a single loop. First calculate the sum for each value and then check for that sum if it is a new maximum and/ or minimum value. If any of these option is true, update the respective maximum or minimum value.

import java.util.*;

public class Application {

    public static void main(String[] args) {
        var test = new double[]{10d, 4.43d, 5.3d, -6.8d, 7.1d, 8.7d, 8.1d, 234d, -3.9d, -455.3d};
        minMaxSum(test);
    }

    public static void minMaxSum(double[] arr) {
        double acc = 0;
        // initialize the minimal value with the maximal value
        double min = Double.MAX_VALUE;
        // initialize the biggest value with the smallest possible value
        double max = Double.MIN_VALUE;
        var sum = new double[arr.length];
        // a single for loop
        for (int i = 0; i < arr.length; i  ) {
            var cur = arr[i];
            // sum up adding the current value
            acc  = cur;
            // save the current value in new array so we can verify the result (not actually required for the algorithm)
            sum[i] = acc;
            // if something is smaller than what we've already encountered => set new smallest value
            if(acc < min) min = acc;
            // if something is bigger than what we've already encountered => set new biggest value
            if(acc < max) max = acc;
        }
        System.out.printf("""
                Original:   %s
                Prefix-sum: %s
                Min: %s
                Max: %s
                """, Arrays.toString(arr), Arrays.toString(sum), min, max);
    }

}

Expected output:

Original:   [10.0, 4.43, 5.3, -6.8, 7.1, 8.7, 8.1, 234.0, -3.9, -455.3]
Prefix-sum: [10.0, 14.43, 19.73, 12.93, 20.03, 28.73, 36.83, 270.83, 266.93, -188.37]
Min: -188.37
Max: 270.83

As many people recommended sorting as a viable approach, here some remarks on sorting. First, yes, it would give you a correct result but there is a price to pay which is speed and well it is just an unnecessarily complex solution.

Sorting based on comparisons and without any pre-conditions cannot be done faster than in O(n log n). It is obvious that the solution above needs O(n) time thus is a complexity class faster. Also the constants (which are ignored in big-O notation) are quite small in the above solution, so even if some pre-conditions were met and you could use a special sorting algorithm like Map sort (or alike) which run in O(n) (again: under certain conditions) the above solution would still be faster and, not to mention, much simpler. So don't make your life harder than it needs to be.

CodePudding user response:

You do not need to use nested loops for getMax or getMin functions.

    public static double getMax(double[] acc){
        double max = Double.MIN_VALUE;
        for (double value : acc) {
            if( value > max) max = value;
        }
        return max;
    }

Faster would be to sort the list:

    public static void miniMaxSum(ArrayList<Integer> arr) {
        if(arr.size() > 0) {
            Collections.sort(arr);
            System.out.println(arr.get(0));
            System.out.println(arr.get(arr.size() - 1));
        }
    }
  • Related