Java程序在数组中查找2个元素,使它们之间的差异最大
数组是最有效的数据结构,旨在存储和访问一组对象。给定一个整数数组,我们的任务是从该数组中找出两个元素,使它们之间的差异最大。
我们将讨论两种方法:
- 通过运行两个循环来比较和检查每个可能对之间的差异。
- 通过计算数组的最小值和最大值并返回它们之间的差异。
Input : arr = {2, 3, 10, 6, 4, 8, 1}
Output : Two elements with largest difference: 10 and 1
Explanation : The maximum difference is between 10 and 1.
Input : arr = {-7, 9, 5, 6, 3, 2}
Output : Two elements with largest difference: 9 and -7
Explanation : The maximum difference is between 9 and -7.
Input : arr = {10, 11, 88, 2, 12, 120}
Output : Two elements with largest difference: 2 and 120
Explanation : The maximum difference is between 2 and 120.
方法 1:通过运行两个循环来比较和检查每个可能对之间的差异
- 使用两个循环。在外循环中,一个一个地选择元素,在内循环中计算所选择元素与数组中每个其他元素的差值。
- 并同时将差值与迄今为止计算出的最大差值进行比较。
- 检查数组中任意 2 个元素之间所有可能的差异,最后选择差异最大的元素。
下面是上述方法的实现:
Java
// Java Program to Find 2
// Elements in an array
// such that the difference
// between them is the largest
import java.io.*;
class Largest_Difference_GFG {
public static int[] Maximum_Diff(int a[], int n)
{
int diff, greatest_diff = a[1] - a[0];
int ele1 = a[1], ele2 = a[0];
// Array to store the difference and the
// two elements ele1 and ele2 .
int res[] = new int[3];
// Check for all possible difference between
// any 2 elements in the array and finally select
// the elements whose difference is the largest
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
diff = Math.abs(a[i] - a[j]);
if (diff > greatest_diff) {
greatest_diff = diff;
ele1 = a[i];
ele2 = a[j];
}
}
}
res[0] = greatest_diff;
res[1] = ele1;
res[2] = ele2;
return (res);
}
public static void main(String[] args)
{
int arr[] = { 10, 11, 88, 2, 12, 120 };
int size = arr.length;
int[] result;
result = Largest_Difference_GFG.Maximum_Diff(arr,
size);
System.out.println("Greatest Difference:"
+ result[0]);
System.out.println(
"Two elements with largest difference: "
+ result[1] + " and " + result[2]);
}
}
Java
// Java Program to Find 2
// Elements in an array
// such that the difference
// between them is the largest
import java.util.*;
class GFG {
public static void main(String args[])
{
int array[] = new int[] { 10, 11, 88, 2, 12, 120 };
int len = array.length;
Arrays.sort(array); // sorting the array
int max_diff = array[len - 1] - array[0];
System.out.println("Maximum Difference is: "
+ max_diff);
System.out.println(
"Two elements with largest difference: "
+ array[0] + " and " + array[len - 1]);
}
}
输出
Greatest Difference:118
Two elements with largest difference: 2 and 120
- 时间复杂度:O(n^2)
- 辅助空间:O(1)
方法二:通过计算数组的最小值和最大值并返回它们之间的差值
- 数组中 2 个元素之间的最大差异将始终是该数组中存在的最小和最大元素之间的绝对差异。
- 使用 2 个独立的 for 循环,确定数组的最小和最大元素。下面是上述方法的实现:
Java
// Java Program to Find 2
// Elements in an array
// such that the difference
// between them is the largest
import java.util.*;
class GFG {
public static void main(String args[])
{
int array[] = new int[] { 10, 11, 88, 2, 12, 120 };
int len = array.length;
Arrays.sort(array); // sorting the array
int max_diff = array[len - 1] - array[0];
System.out.println("Maximum Difference is: "
+ max_diff);
System.out.println(
"Two elements with largest difference: "
+ array[0] + " and " + array[len - 1]);
}
}
输出
Maximum Difference is: 118
Two elements with largest difference: 2 and 120
- 时间复杂度:O(n)
- 辅助空间:O(1)
注意:上述代码中的最小值和最大值元素也可以在单个 for 循环中通过将数组的每个元素与 minValue 和 maxValue 进行比较并相应地更新它们来确定。这样,一个for循环就可以同时返回minValue和maxValue,代码行数会减少。