📜  前缀和后缀乘积相等的索引计数

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

前缀和后缀乘积相等的索引计数

给定一个整数数组arr[] ,任务是找到前缀乘积和后缀乘积相等的索引数。

例子:

朴素方法:给定的问题可以通过从左到右遍历数组arr并计算前缀乘积直到该索引然后从右到左迭代数组arr并计算后缀乘积然后检查前缀和后缀乘积是否相等来解决。
时间复杂度: O(N^2)

有效的方法:上述方法可以通过使用 哈希技术。请按照以下步骤解决问题:

  • 从右到左遍历数组arr并在每个索引处将产品存储到辅助数组prod
  • 从左到右迭代数组arr并在每个索引处计算前缀乘积
  • 对于获得的每个前缀产品,检查产品中是否存在相同值的后缀产品
    • 如果是,则将计数res增加 1
  • 返回res得到的结果

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to calculate number of
// equal prefix and suffix product
// till the same indices
int equalProdPreSuf(vector& arr)
{
 
    // Initialize a variable
    // to store the result
    int res = 0;
 
    // Initialize variables to
    // calculate prefix and suffix sums
    int preProd = 1, sufProd = 1;
 
    // Length of array arr
    int len = arr.size();
 
    // Initialize an auxiliary array to
    // store suffix product at every index
    vector prod(len, 0);
 
    // Traverse the array from right to left
    for (int i = len - 1; i >= 0; i--) {
 
        // Multiply the current
        // element to sufSum
        sufProd *= arr[i];
 
        // Store the value in prod
        prod[i] = sufProd;
    }
 
    // Iterate the array from left to right
    for (int i = 0; i < len; i++) {
 
        // Multiply the current
        // element to preProd
        preProd *= arr[i];
 
        // If prefix product is equal to
        // suffix product prod[i] then
        // increment res by 1
        if (preProd == prod[i]) {
 
            // Increment the result
            res++;
        }
    }
 
    // Return the answer
    return res;
}
 
// Driver code
int main()
{
 
    // Initialize the array
    vector arr = { 4, 5, 1, 1, -2, 5, -2 };
 
    // Call the function and
    // print its result
    cout << equalProdPreSuf(arr);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java implementation for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to calculate number of
    // equal prefix and suffix product
    // till the same indices
    public static int equalProdPreSuf(int[] arr)
    {
 
        // Initialize a variable
        // to store the result
        int res = 0;
 
        // Initialize variables to
        // calculate prefix and suffix sums
        int preProd = 1, sufProd = 1;
 
        // Length of array arr
        int len = arr.length;
 
        // Initialize an auxiliary array to
        // store suffix product at every index
        int[] prod = new int[len];
 
        // Traverse the array from right to left
        for (int i = len - 1; i >= 0; i--) {
 
            // Multiply the current
            // element to sufSum
            sufProd *= arr[i];
 
            // Store the value in prod
            prod[i] = sufProd;
        }
 
        // Iterate the array from left to right
        for (int i = 0; i < len; i++) {
 
            // Multiply the current
            // element to preProd
            preProd *= arr[i];
 
            // If prefix product is equal to
            // suffix product prod[i] then
            // increment res by 1
            if (preProd == prod[i]) {
 
                // Increment the result
                res++;
            }
        }
 
        // Return the answer
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Initialize the array
        int[] arr = { 4, 5, 1, 1, -2, 5, -2 };
 
        // Call the function and
        // print its result
        System.out.println(equalProdPreSuf(arr));
    }
}


Python3
# Python Program to implement
# the above approach
 
# Function to calculate number of
# equal prefix and suffix product
# till the same indices
def equalProdPreSuf(arr):
 
    # Initialize a variable
    # to store the result
    res = 0
 
    # Initialize variables to
    # calculate prefix and suffix sums
    preProd = 1
    sufProd = 1
 
    # Length of array arr
    Len = len(arr)
 
    # Initialize an auxiliary array to
    # store suffix product at every index
    prod = [0] * Len
 
    # Traverse the array from right to left
    for i in range(Len-1, 0, -1):
 
        # Multiply the current
        # element to sufSum
        sufProd *= arr[i]
 
        # Store the value in prod
        prod[i] = sufProd
 
    # Iterate the array from left to right
    for i in range(Len):
 
        # Multiply the current
        # element to preProd
        preProd *= arr[i]
 
        # If prefix product is equal to
        # suffix product prod[i] then
        # increment res by 1
        if (preProd == prod[i]):
 
            # Increment the result
            res += 1
 
    # Return the answer
    return res
 
 
# Driver code
 
# Initialize the array
arr = [4, 5, 1, 1, -2, 5, -2]
 
# Call the function and
# print its result
print(equalProdPreSuf(arr))
 
# This code is contributed by gfgking.


C#
// C# implementation for the above approach
using System;
 
class GFG {
 
    // Function to calculate number of
    // equal prefix and suffix product
    // till the same indices
    public static int equalProdPreSuf(int[] arr)
    {
 
        // Initialize a variable
        // to store the result
        int res = 0;
 
        // Initialize variables to
        // calculate prefix and suffix sums
        int preProd = 1, sufProd = 1;
 
        // Length of array arr
        int len = arr.Length;
 
        // Initialize an auxiliary array to
        // store suffix product at every index
        int[] prod = new int[len];
 
        // Traverse the array from right to left
        for (int i = len - 1; i >= 0; i--) {
 
            // Multiply the current
            // element to sufSum
            sufProd *= arr[i];
 
            // Store the value in prod
            prod[i] = sufProd;
        }
 
        // Iterate the array from left to right
        for (int i = 0; i < len; i++) {
 
            // Multiply the current
            // element to preProd
            preProd *= arr[i];
 
            // If prefix product is equal to
            // suffix product prod[i] then
            // increment res by 1
            if (preProd == prod[i]) {
 
                // Increment the result
                res++;
            }
        }
 
        // Return the answer
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        // Initialize the array
        int[] arr = { 4, 5, 1, 1, -2, 5, -2 };
 
        // Call the function and
        // print its result
        Console.Write(equalProdPreSuf(arr));
    }
}
 
// This code is contributed by gfgking.


Javascript



输出
2

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