📜  在第二大产品的数组中查找对

📅  最后修改于: 2021-04-22 00:03:15             🧑  作者: Mango

给定N个整数的数组arr [] ,其中N> 2 ,任务是从给定数组中查找第二大乘积对。

例子:

天真的方法:天真的方法是从给定的数组中生成所有可能的对,并将具有该对的乘积插入对的集合中。将所有配对产品插入套装后,再打印套装的倒数第二个产品。步骤如下:

  1. 通过给定的数组制作一对对及其乘积。
  2. 将所有对插入向量对中。
  3. 如果向量大小为1,则打印该对,否则在向量的第(总向量大小– 2)位置打印该对。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find second largest
// product of pairs
void secondLargerstPair(int arr[], int N)
{
  
    // If size of array is less then 3
    // then second largest product pair
    // dose not exits.
    if (N < 3)
        return;
  
    // Declaring set of pairs which
    // contains possible pairs of array
    // and their products
    set > > s;
  
    // Declaring vector of pairs
    vector > v;
  
    for (int i = 0; i < N; ++i) {
  
        for (int j = i + 1; j < N; ++j) {
  
            // Inserting a set
            s.insert(make_pair(arr[i] * arr[j],
                               make_pair(arr[i],
                                         arr[j])));
        }
    }
  
    // Traverse set of pairs
    for (auto i : s) {
  
        // Inserting values in vector
        // of pairs
        v.push_back(
            make_pair((i.second).first,
                      (i.second).second));
    }
  
    int vsize = v.size();
  
    // Printing the result
    cout << v[vsize - 2].first << " "
         << v[vsize - 2].second << endl;
}
  
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
  
    // Size of Array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    secondLargerstPair(arr, N);
    return 0;
}


C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find second largest
// product pair in arr[0..n-1]
void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) {
        return;
    }
  
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
  
    // Traverse through every possible pair
    // and keep track of largest product
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++) {
  
            // If pair is largest
            if (arr[i] * arr[j] > a * b) {
  
                // Second largest
                c = a, d = b;
                a = arr[i], b = arr[j];
            }
  
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b
                && arr[i] * arr[j] > c * d)
                c = arr[i], d = arr[j];
        }
  
    // Print the pairs
    cout << c << " " << d;
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    maxProduct(arr, N);
    return 0;
}


Java
// Java program for the above approach
class GFG{ 
    
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3)
    {
        return;
    }
   
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
   
    // Traverse through every possible pair
    // and keep track of largest product
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N-1; j++) 
        {
   
            // If pair is largest
            if (arr[i] * arr[j] > a * b) 
            {
   
                // Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
            }
   
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b && 
                arr[i] * arr[j] > c * d)
                c = arr[i];
                d = arr[j];
        }
   
    // Print the pairs
    System.out.println(c + " " + d);
}
   
// Driver Code
public static void main(String[] args)
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
    int N = arr.length;
   
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by Ritik Bansal


Python3
# Python3 program for the above approach
  
# Function to find second largest
# product pair in arr[0..n-1]
def maxProduct(arr, N):
      
    # No pair exits
    if (N < 3):
        return;
      
    # Initialize max product pair
    a = arr[0]; b = arr[1];
    c = 0; d = 0;
  
    # Traverse through every possible pair
    # and keep track of largest product
    for i in range(0, N, 1):
        for j in range(i + 1, N - 1, 1):
  
            # If pair is largest
            if (arr[i] * arr[j] > a * b):
  
                # Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
              
            # If pair dose not largest but
            # larger then second largest
            if (arr[i] * arr[j] < a * b and
                arr[i] * arr[j] > c * d):
                c = arr[i];
                  
            d = arr[j];
          
    # Print the pairs
    print(c, " ", d);
  
# Driver Code
if __name__ == '__main__':
      
    # Given array
    arr = [ 5, 2, 67, 45, 160, 78];
    N = len(arr);
  
    # Function call
    maxProduct(arr, N);
  
# This code is contributed by Amit Katiyar


C#
// C# program for the above approach
using System;
  
class GFG{ 
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int []arr, int N)
{
      
    // No pair exits
    if (N < 3)
    {
        return;
    }
  
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
  
    // Traverse through every possible pair
    // and keep track of largest product
    for(int i = 0; i < N; i++)
        for(int j = i + 1; j < N - 1; j++) 
        {
              
            // If pair is largest
            if (arr[i] * arr[j] > a * b) 
            {
  
                // Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
            }
  
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b && 
                arr[i] * arr[j] > c * d)
                c = arr[i];
                d = arr[j];
        }
  
    // Print the pairs
    Console.WriteLine(c + " " + d);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array
    int []arr = { 5, 2, 67, 45, 160, 78 };
    int N = arr.Length;
  
    // Function call
    maxProduct(arr, N);
}
}
  
// This code is contributed by 29AjayKumar


C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find second largest
// product pair in arr[0..n-1]
void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) {
        return;
    }
  
    // Sort the array
    sort(arr, arr + N);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3
        >= largest1 * largest3) {
        cout << smallest1 << " " << smallest3;
    }
    else {
        cout << largest1 << " " << largest3;
    }
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    maxProduct(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) 
    {
        return;
    }
  
    // Sort the array
    Arrays.sort(arr);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3 >= 
        largest1 * largest3) 
    {
        System.out.print(smallest1 + " " + 
                         smallest3);
    }
    else 
    {
        System.out.print(largest1 + " " +  
                         largest3);
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
  
    int N = arr.length;
  
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
  
# Function to find second largest
# product pair in arr[0..n-1]
def maxProduct(arr, N):
    
    # No pair exits
    if (N < 3):
        return;
  
    # Sort the array
    arr.sort();
  
    # Initialize smallest element
    # of the array
    smallest1 = arr[0];
    smallest3 = arr[2];
  
    # Initialize largest element
    # of the array
    largest1 = arr[N - 1];
    largest3 = arr[N - 3];
  
    # Prsecond largest product pair
    if (smallest1 * 
        smallest3 >= largest1 * 
                     largest3):
        print(smallest1 , " " , smallest3);
    else:
        print(largest1 , " " , largest3);
  
# Driver Code
if __name__ == '__main__':
    
    # Given array
    arr = [5, 2, 67, 45, 160, 78];
  
    N = len(arr);
  
    # Function Call
    maxProduct(arr, N);
  
# This code is contributed by sapnasingh4991


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int []arr, int N)
{
    // No pair exits
    if (N < 3) 
    {
        return;
    }
  
    // Sort the array
    Array.Sort(arr);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3 >= 
        largest1 * largest3) 
    {
        Console.Write(smallest1 + " " + 
                      smallest3);
    }
    else 
    {
        Console.Write(largest1 + " " +  
                      largest3);
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given array
    int []arr = { 5, 2, 67, 45, 160, 78 };
  
    int N = arr.Length;
  
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by Rohit_ranjan


输出:
67 160

时间复杂度: O(N 2 )
辅助空间: O(N)

更好的解决方案:更好的解决方案是遍历数组的所有对,并在遍历时存储最大和第二大的产品对。遍历后,打印存储的第二大对的对。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
  
// Function to find second largest
// product pair in arr[0..n-1]
void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) {
        return;
    }
  
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
  
    // Traverse through every possible pair
    // and keep track of largest product
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++) {
  
            // If pair is largest
            if (arr[i] * arr[j] > a * b) {
  
                // Second largest
                c = a, d = b;
                a = arr[i], b = arr[j];
            }
  
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b
                && arr[i] * arr[j] > c * d)
                c = arr[i], d = arr[j];
        }
  
    // Print the pairs
    cout << c << " " << d;
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    maxProduct(arr, N);
    return 0;
}

Java

// Java program for the above approach
class GFG{ 
    
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3)
    {
        return;
    }
   
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
   
    // Traverse through every possible pair
    // and keep track of largest product
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N-1; j++) 
        {
   
            // If pair is largest
            if (arr[i] * arr[j] > a * b) 
            {
   
                // Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
            }
   
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b && 
                arr[i] * arr[j] > c * d)
                c = arr[i];
                d = arr[j];
        }
   
    // Print the pairs
    System.out.println(c + " " + d);
}
   
// Driver Code
public static void main(String[] args)
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
    int N = arr.length;
   
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by Ritik Bansal

Python3

# Python3 program for the above approach
  
# Function to find second largest
# product pair in arr[0..n-1]
def maxProduct(arr, N):
      
    # No pair exits
    if (N < 3):
        return;
      
    # Initialize max product pair
    a = arr[0]; b = arr[1];
    c = 0; d = 0;
  
    # Traverse through every possible pair
    # and keep track of largest product
    for i in range(0, N, 1):
        for j in range(i + 1, N - 1, 1):
  
            # If pair is largest
            if (arr[i] * arr[j] > a * b):
  
                # Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
              
            # If pair dose not largest but
            # larger then second largest
            if (arr[i] * arr[j] < a * b and
                arr[i] * arr[j] > c * d):
                c = arr[i];
                  
            d = arr[j];
          
    # Print the pairs
    print(c, " ", d);
  
# Driver Code
if __name__ == '__main__':
      
    # Given array
    arr = [ 5, 2, 67, 45, 160, 78];
    N = len(arr);
  
    # Function call
    maxProduct(arr, N);
  
# This code is contributed by Amit Katiyar 

C#

// C# program for the above approach
using System;
  
class GFG{ 
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int []arr, int N)
{
      
    // No pair exits
    if (N < 3)
    {
        return;
    }
  
    // Initialize max product pair
    int a = arr[0], b = arr[1];
    int c = 0, d = 0;
  
    // Traverse through every possible pair
    // and keep track of largest product
    for(int i = 0; i < N; i++)
        for(int j = i + 1; j < N - 1; j++) 
        {
              
            // If pair is largest
            if (arr[i] * arr[j] > a * b) 
            {
  
                // Second largest
                c = a;
                d = b;
                a = arr[i];
                b = arr[j];
            }
  
            // If pair dose not largest but
            // larger then second largest
            if (arr[i] * arr[j] < a * b && 
                arr[i] * arr[j] > c * d)
                c = arr[i];
                d = arr[j];
        }
  
    // Print the pairs
    Console.WriteLine(c + " " + d);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array
    int []arr = { 5, 2, 67, 45, 160, 78 };
    int N = arr.Length;
  
    // Function call
    maxProduct(arr, N);
}
}
  
// This code is contributed by 29AjayKumar
输出:
67 160

时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:

  1. 对数组进行排序。
  2. 查找用于处理负数的第一和第三最小元素。
  3. 找到用于处理正数的第一和第三大元素。
  4. 比较最小对和最大对的乘积。
  5. 返回其中最大的一个。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
  
// Function to find second largest
// product pair in arr[0..n-1]
void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) {
        return;
    }
  
    // Sort the array
    sort(arr, arr + N);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3
        >= largest1 * largest3) {
        cout << smallest1 << " " << smallest3;
    }
    else {
        cout << largest1 << " " << largest3;
    }
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    maxProduct(arr, N);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int arr[], int N)
{
    // No pair exits
    if (N < 3) 
    {
        return;
    }
  
    // Sort the array
    Arrays.sort(arr);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3 >= 
        largest1 * largest3) 
    {
        System.out.print(smallest1 + " " + 
                         smallest3);
    }
    else 
    {
        System.out.print(largest1 + " " +  
                         largest3);
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given array
    int arr[] = { 5, 2, 67, 45, 160, 78 };
  
    int N = arr.length;
  
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by gauravrajput1

Python3

# Python3 program for the above approach
  
# Function to find second largest
# product pair in arr[0..n-1]
def maxProduct(arr, N):
    
    # No pair exits
    if (N < 3):
        return;
  
    # Sort the array
    arr.sort();
  
    # Initialize smallest element
    # of the array
    smallest1 = arr[0];
    smallest3 = arr[2];
  
    # Initialize largest element
    # of the array
    largest1 = arr[N - 1];
    largest3 = arr[N - 3];
  
    # Prsecond largest product pair
    if (smallest1 * 
        smallest3 >= largest1 * 
                     largest3):
        print(smallest1 , " " , smallest3);
    else:
        print(largest1 , " " , largest3);
  
# Driver Code
if __name__ == '__main__':
    
    # Given array
    arr = [5, 2, 67, 45, 160, 78];
  
    N = len(arr);
  
    # Function Call
    maxProduct(arr, N);
  
# This code is contributed by sapnasingh4991

C#

// C# program for the above approach
using System;
class GFG{
  
// Function to find second largest
// product pair in arr[0..n-1]
static void maxProduct(int []arr, int N)
{
    // No pair exits
    if (N < 3) 
    {
        return;
    }
  
    // Sort the array
    Array.Sort(arr);
  
    // Initialize smallest element
    // of the array
    int smallest1 = arr[0];
    int smallest3 = arr[2];
  
    // Initialize largest element
    // of the array
    int largest1 = arr[N - 1];
    int largest3 = arr[N - 3];
  
    // Print second largest product pair
    if (smallest1 * smallest3 >= 
        largest1 * largest3) 
    {
        Console.Write(smallest1 + " " + 
                      smallest3);
    }
    else 
    {
        Console.Write(largest1 + " " +  
                      largest3);
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given array
    int []arr = { 5, 2, 67, 45, 160, 78 };
  
    int N = arr.Length;
  
    // Function Call
    maxProduct(arr, N);
}
}
  
// This code is contributed by Rohit_ranjan
输出:
160 67

时间复杂度: O(N * log N)
辅助空间: O(1)