📌  相关文章
📜  最大化从两个给定数组获得的等长子数组的相同索引元素的乘积总和

📅  最后修改于: 2021-09-06 17:44:41             🧑  作者: Mango

给定两个大小分别为NM 个整数的数组arr[]brr[] ,任务是最大化两个等长子数组的相同索引元素与从数组brr[ 中选择的子数组的乘积之和]被逆转。

例子:

方法:给定的问题可以通过将两个数组中每个元素的乘积存储在一个二维矩阵中来解决 并在此二维矩阵的每个右对角线上找到具有最大和的子数组,这类似于在数组中找到最大和子数组。请按照以下步骤解决问题:

  • 初始化大小为N*M的二维矩阵mat[][]以存储两个数组中每个元素的乘积。
  • 从给定的数组arr[]brr[]生成所有可能的对,并将它们存储在二维矩阵mat[][] 中
  • 现在,对于相同长度的子数组,想法是从第一行第一列开始遍历矩阵的对角线。
  • 每次遍历对角线后,将元素存储在数组中,然后找到数组中存储元素的最大和子数组。
  • 完成以上步骤后,打印以上步骤得到的所有最大和中的最大和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to store product of each
// pair of elements from two arrays
void store_in_matrix(int a[], int b[],
                     int n1, int n2,
                     int mat[][10])
{
    // Store product of pairs
    // of elements in a matrix
    for (int i = 0; i < n1; i++) {
        for (int j = 0; j < n2; j++) {
 
            mat[i][j] = (a[i] * b[j]);
        }
    }
}
 
// Function to find the maximum subarray
// sum in every right diagonal of the matrix
void maxsum_rt_diag(int n1, int n2,
                    int mat[][10], int& ans)
{
    // Stores maximum continuous sum
    int max_ending_here;
 
    int i, j;
 
    // Start with each element
    // from the last column
    for (int t = 0; t < n1; t++) {
 
        i = t;
        j = n2 - 1;
        max_ending_here = 0;
 
        // Check for each diagonal
        while (i < n1 && j >= 0) {
 
            max_ending_here = max_ending_here
                              + mat[i][j];
            i++;
            j--;
 
            // Update ans if max_ending_here
            // is greater than ans
            if (ans < max_ending_here)
                ans = max_ending_here;
 
            // If max_ending_here is -ve
            if (max_ending_here < 0)
 
                // Reset it to 0
                max_ending_here = 0;
        }
    }
 
    // Start with each element
    // from the first row
    for (int t = 0; t < n2; t++) {
        i = 0;
        j = t;
        max_ending_here = 0;
 
        // Check for each diagonal
        while (i < n1 && j >= 0) {
 
            max_ending_here = max_ending_here
                              + mat[i][j];
            i++;
            j--;
 
            // Update ans if max_ending_here
            // is greater than ans
            if (ans < max_ending_here)
                ans = max_ending_here;
 
            // If max_ending_here is -ve
            if (max_ending_here < 0)
 
                // Reset to 0
                max_ending_here = 0;
        }
    }
}
 
// Function to initialize matrix to 0
void initMatrix(int mat[10][10],
                int n1, int n2)
{
    // Traverse each row
    for (int i = 0; i < n1; i++) {
 
        // Traverse each column
        for (int j = 0; j < n2; j++) {
            mat[i][j] = 0;
        }
    }
}
 
// Function to find the maximum sum of
// the two equal subarray selected from
// the given two arrays a[] and b[]
void findMaxProduct(int a[], int n1,
                    int b[], int n2)
{
 
    // Stores the matrix
    int mat[10][10];
 
    // Initialize each element in mat[] to 0
    initMatrix(mat, n1, n2);
 
    // Store product of each element
    // from two arrays in a matrix
    store_in_matrix(a, b, n1, n2, mat);
 
    // Stores the result
    int ans = 0;
 
    // Find maximum subarray sum in
    // every right diagonal of matrix
    maxsum_rt_diag(n1, n2, mat, ans);
 
    // Print the maximum sum
    cout << ans << "\n";
}
 
// Driver Code
int main()
{
    // Initialize two arrays
    int arr[] = { -1, 3, -2, 4, 5 };
    int brr[] = { 4, -5 };
 
    // Find size of array
    int N = sizeof(arr) / sizeof(arr[0]);
    int M = sizeof(brr) / sizeof(brr[0]);
 
    // Function Call
    findMaxProduct(arr, N, brr, M);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to store product of each
    // pair of elements from two arrays
    static void store_in_matrix(int a[], int b[], int n1,
                                int n2, int mat[][])
    {
        // Store product of pairs
        // of elements in a matrix
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
 
                mat[i][j] = (a[i] * b[j]);
            }
        }
    }
 
    // Function to find the maximum subarray
    // sum in every right diagonal of the matrix
    static int maxsum_rt_diag(int n1, int n2, int mat[][])
    {
        // Stores maximum continuous sum
        int max_ending_here;
 
        int i, j;
 
        // Stores the result
        int ans = 0;
 
        // Start with each element
        // from the last column
        for (int t = 0; t < n1; t++) {
 
            i = t;
            j = n2 - 1;
            max_ending_here = 0;
 
            // Check for each diagonal
            while (i < n1 && j >= 0) {
 
                max_ending_here
                    = max_ending_here + mat[i][j];
                i++;
                j--;
 
                // Update ans if max_ending_here
                // is greater than ans
                if (ans < max_ending_here)
                    ans = max_ending_here;
 
                // If max_ending_here is -ve
                if (max_ending_here < 0)
 
                    // Reset it to 0
                    max_ending_here = 0;
            }
        }
 
        // Start with each element
        // from the first row
        for (int t = 0; t < n2; t++) {
            i = 0;
            j = t;
            max_ending_here = 0;
 
            // Check for each diagonal
            while (i < n1 && j >= 0) {
 
                max_ending_here
                    = max_ending_here + mat[i][j];
                i++;
                j--;
 
                // Update ans if max_ending_here
                // is greater than ans
                if (ans < max_ending_here)
                    ans = max_ending_here;
 
                // If max_ending_here is -ve
                if (max_ending_here < 0)
 
                    // Reset to 0
                    max_ending_here = 0;
            }
        }
 
        return ans;
    }
 
    // Function to find the maximum sum of
    // the two equal subarray selected from
    // the given two arrays a[] and b[]
    static void findMaxProduct(int a[], int n1, int b[],
                               int n2)
    {
 
        // Stores the matrix
        int mat[][] = new int[10][10];
 
        // Store product of each element
        // from two arrays in a matrix
        store_in_matrix(a, b, n1, n2, mat);
 
        // Stores the result
        int ans = 0;
 
        // Find maximum subarray sum in
        // every right diagonal of matrix
        ans = maxsum_rt_diag(n1, n2, mat);
 
        // Print the maximum sum
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Initialize two arrays
        int arr[] = { -1, 3, -2, 4, 5 };
        int brr[] = { 4, -5 };
 
        // Find size of array
        int N = arr.length;
        int M = brr.length;
 
        // Function Call
        findMaxProduct(arr, N, brr, M);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to store product of each
# pair of elements from two arrays
def store_in_matrix(a, b, n1, n2, mat):
   
    # Store product of pairs
    # of elements in a matrix
    for i in range(n1):
        for j in range(n2):
            mat[i][j] = (a[i] * b[j])
 
# Function to find the maximum subarray
# sum in every right diagonal of the matrix
def maxsum_rt_diag(n1, n2, mat, ans):
    # Stores maximum continuous sum
    max_ending_here=0
 
    i, j = 0, 0
 
    # Start with each element
    # from the last column
    for t in range(n1):
 
        i = t
        j = n2 - 1
        max_ending_here = 0
 
        # Check for each diagonal
        while (i < n1 and j >= 0):
 
            max_ending_here = max_ending_here + mat[i][j]
            i += 1
            j -= 1
 
            # Update ans if max_ending_here
            # is greater than ans
            if (ans < max_ending_here):
                ans = max_ending_here
 
            # If max_ending_here is -ve
            if (max_ending_here < 0):
 
                # Reset it to 0
                max_ending_here = 0
 
 
    # Start with each element
    # from the first row
    for t in range(n2):
        i = 0
        j = t
        max_ending_here = 0
 
        # Check for each diagonal
        while (i < n1 and j >= 0):
 
            max_ending_here = max_ending_here + mat[i][j]
            i += 1
            j -= 1
 
            # Update ans if max_ending_here
            # is greater than ans
            if (ans < max_ending_here):
                ans = max_ending_here
 
            # If max_ending_here is -ve
            if (max_ending_here < 0):
               
                # Reset to 0
                max_ending_here = 0
    return ans
 
# Function to initialize matrix to 0
def initMatrix(mat, n1, n2):
   
    # Traverse each row
    for i in range(n1):
       
        # Traverse each column
        for j in range(n2):
            mat[i][j] = 0
 
# Function to find the maximum sum of
# the two equal subarray selected from
# the given two arrays a[] and b[]
def findMaxProduct(a, n1, b, n2):
 
    # Stores the matrix
    mat = [[ 0 for i in range(10)] for i in range(10)]
 
    # Initialize each element in mat[] to 0
    initMatrix(mat, n1, n2)
 
    # Store product of each element
    # from two arrays in a matrix
    store_in_matrix(a, b, n1, n2, mat)
 
    # Stores the result
    ans = 0
 
    # Find maximum subarray sum in
    # every right diagonal of matrix
    ans = maxsum_rt_diag(n1, n2, mat, ans)
 
    # Print the maximum sum
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    # Initialize two arrays
    arr= [-1, 3, -2, 4, 5]
    brr= [4, -5]
 
    # Find size of array
    N = len(arr)
    M = len(brr)
 
    # Function Call
    findMaxProduct(arr, N, brr, M)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG {
 
    // Function to store product of each
    // pair of elements from two arrays
    static void store_in_matrix(int[] a, int[] b, int n1,
                                int n2, int[, ] mat)
    {
        // Store product of pairs
        // of elements in a matrix
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
 
                mat[i, j] = (a[i] * b[j]);
            }
        }
    }
 
    // Function to find the maximum subarray
    // sum in every right diagonal of the matrix
    static int maxsum_rt_diag(int n1, int n2, int[, ] mat)
    {
        // Stores maximum continuous sum
        int max_ending_here;
 
        int i, j;
 
        // Stores the result
        int ans = 0;
 
        // Start with each element
        // from the last column
        for (int t = 0; t < n1; t++) {
 
            i = t;
            j = n2 - 1;
            max_ending_here = 0;
 
            // Check for each diagonal
            while (i < n1 && j >= 0) {
 
                max_ending_here
                    = max_ending_here + mat[i, j];
                i++;
                j--;
 
                // Update ans if max_ending_here
                // is greater than ans
                if (ans < max_ending_here)
                    ans = max_ending_here;
 
                // If max_ending_here is -ve
                if (max_ending_here < 0)
 
                    // Reset it to 0
                    max_ending_here = 0;
            }
        }
 
        // Start with each element
        // from the first row
        for (int t = 0; t < n2; t++) {
            i = 0;
            j = t;
            max_ending_here = 0;
 
            // Check for each diagonal
            while (i < n1 && j >= 0) {
 
                max_ending_here
                    = max_ending_here + mat[i, j];
                i++;
                j--;
 
                // Update ans if max_ending_here
                // is greater than ans
                if (ans < max_ending_here)
                    ans = max_ending_here;
 
                // If max_ending_here is -ve
                if (max_ending_here < 0)
 
                    // Reset to 0
                    max_ending_here = 0;
            }
        }
 
        return ans;
    }
 
    // Function to find the maximum sum of
    // the two equal subarray selected from
    // the given two arrays a[] and b[]
    static void findMaxProduct(int[] a, int n1, int[] b,
                               int n2)
    {
 
        // Stores the matrix
        int[, ] mat = new int[10, 10];
 
        // Store product of each element
        // from two arrays in a matrix
        store_in_matrix(a, b, n1, n2, mat);
 
        // Stores the result
        int ans = 0;
 
        // Find maximum subarray sum in
        // every right diagonal of matrix
        ans = maxsum_rt_diag(n1, n2, mat);
 
        // Print the maximum sum
        Console.WriteLine(ans);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        // Initialize two arrays
        int[] arr = { -1, 3, -2, 4, 5 };
        int[] brr = { 4, -5 };
 
        // Find size of array
        int N = arr.Length;
        int M = brr.Length;
 
        // Function Call
        findMaxProduct(arr, N, brr, M);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
26

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live