数组中查找最大值_查找数组中的最大绝对差

数组中查找最大值

Given an array and we have to find maximum absolute difference.

给定一个数组,我们必须找到最大的绝对差。

Approach to follow:

遵循的方法:

  • In the first step, we take input an array with few elements.

    第一步,我们输入一个包含很少元素的数组。

            int[] array = {10,20,50,80,90};
    
  • In the second step, we will find the maximum and minimum element of an array.

    在第二步中,我们将找到数组的最大和最小元素。

  • In the third step, we will subtract the minimum element from the maximum element of an array so the difference between the minimum and maximum element of an array is the maximum absolute difference of an array.

    在第三步中,我们将从数组的最大元素中减去最小元素,以便数组的最小元素和最大元素之间的差是数组的最大绝对差。

Example:

例:

// Java program to find the maximum absolute difference 
// of an array

class MaximumAbsoluteDifferenceOfArray {
    public static void main(String[] args) {
        // Declare and initialize an array
        int[] array = {
            10,
            20,
            50,
            80
        };
        
        int num_of_elements = array.length;
        
        // To store the minimum and the maximum elements 
        // from the array and assigning first element 
        int min = array[0];
        int max = array[0];
        
        for (int i = 1; i < num_of_elements; i++) {
            // We are comparing first element with all other elements
            min = Math.min(min, array[i]);
            max = Math.max(max, array[i]);
        }
        
        int abs_diff = max - min;
        System.out.println("The maximum absolute difference of an array is " + abs_diff);
    }
}

Output

输出量

E:\Programs>javac MaximumAbsoluteDifferenceOfArray.java

E:\Programs>java MaximumAbsoluteDifferenceOfArray
The maximum absolute difference of an array is 70


翻译自: https://www.includehelp.com/java/find-maximum-absolute-difference-in-an-array.aspx

数组中查找最大值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值