📜  Java程序最小产品对正整数数组

📅  最后修改于: 2022-05-13 01:54:57.831000             🧑  作者: Mango

Java程序最小产品对正整数数组

给定一个正整数数组。我们需要编写一个程序来打印给定数组的任意两个数字的最小乘积。
例子:

Input : 11 8 5 7 5 100
Output : 25 
Explanation : The minimum product of any 
two numbers will be 5 * 5 = 25.

Input : 198 76 544 123 154 675 
Output : 7448
Explanation : The minimum product of any 
two numbers will be 76 * 123 = 7448.

简单方法:一种简单的方法是运行两个嵌套循环来生成所有可能的元素对并跟踪最小乘积。
时间复杂度:O(n * n)
辅助空间:O(1)
更好的方法:一种有效的方法是首先对给定数组进行排序并打印前两个数字的乘积,排序将花费 O(n log n)。答案将是 a[0] * a[1]
时间复杂度:O(n * log(n))
辅助空间:O(1)
最佳方法:这个想法是线性遍历给定数组并跟踪最少两个元素。最后返回两个最小元素的乘积。
下面是上述方法的实现。

Java
// Java program to calculate minimum
// product of a pair
import java.util.*;
  
class GFG {
      
    // Function to calculate minimum product
    // of pair
    static int printMinimumProduct(int arr[], int n)
    {
        // Initialize first and second
        // minimums. It is assumed that the
        // array has at least two elements.
        int first_min = Math.min(arr[0], arr[1]);
        int second_min = Math.max(arr[0], arr[1]);
       
        // Traverse remaining array and keep
        // track of two minimum elements (Note
        // that the two minimum elements may
        // be same if minimum element appears
        // more than once)
        // more than once)
        for (int i = 2; i < n; i++)
        {
           if (arr[i] < first_min)
           {
              second_min = first_min;
              first_min = arr[i];
           }
           else if (arr[i] < second_min)
              second_min = arr[i];
        }
       
        return first_min * second_min;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int a[] = { 11, 8 , 5 , 7 , 5 , 100 };
        int n = a.length;
        System.out.print(printMinimumProduct(a,n));
       
    }
}
  
// This code is contributed by Arnav Kr. Mandal.


输出:

25

时间复杂度:O(n)
辅助空间:O(1)
有关更多详细信息,请参阅有关最小产品对一组正整数的完整文章!