Skip to content
Related Articles
Open in App
Not now

Related Articles

Count of indices for which the prefix and suffix product are equal

Improve Article
Save Article
  • Last Updated : 30 Nov, 2021
Improve Article
Save Article

Given an array arr[] of integers, the task is to find the number of indices for which the prefix product and the suffix product are equal.

Example: 

Input: arr = [4, -5, 1, 1, -2, 5, -2]
Output: 2
Explanation:  The indices on which the prefix and the suffix product are equal are given below:
At index 2 prefix and suffix product are 20
At index 3 prefix and suffix product are 20

Input: arr = [5, 0, 4, -1, -3, 0]
Output: 3
Explanation:  The indices on which the prefix and the suffix product are equal are given below:
At index 1 prefix and suffix product are 0
At index 2 prefix and suffix product are 0
At index 3 prefix and suffix product are 0
At index 4 prefix and suffix product are 0
At index 5 prefix and suffix product are 0

 

Naive Approach: The given problem can be solved by traversing the array arr from left to right and calculating prefix product till that index then iterating the array arr from right to left and calculating the suffix product then checking if prefix and suffix product are equal.
Time Complexity: O(N^2)

Efficient Approach: The above approach can be solved by using the Hashing technique. Follow the steps below to solve the problem:

  • Traverse the array arr from right to left and at every index store the product into an auxiliary array prod
  • Iterate the array arr from left to right and at every index calculate the prefix product
  • For every prefix product obtained, check suffix product of the same value is present in prod
    • If yes, then increment the count res by 1
  • Return the result res obtained

Below is the implementation of the above approach:

C++




// C++ implementation for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate number of
// equal prefix and suffix product
// till the same indices
int equalProdPreSuf(vector<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.size();
 
    // Initialize an auxiliary array to
    // store suffix product at every index
    vector<int> 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<int> 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




<script>
 
       // JavaScript Program to implement
       // the above approach
 
       // Function to calculate number of
       // equal prefix and suffix product
       // till the same indices
       function equalProdPreSuf(arr) {
 
           // Initialize a variable
           // to store the result
           let res = 0;
 
           // Initialize variables to
           // calculate prefix and suffix sums
           let preProd = 1, sufProd = 1;
 
           // Length of array arr
           let len = arr.length;
 
           // Initialize an auxiliary array to
           // store suffix product at every index
           let prod = new Array(len).fill(0);
 
           // Traverse the array from right to left
           for (let 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 (let 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
 
       // Initialize the array
       let arr = [4, 5, 1, 1, -2, 5, -2];
 
       // Call the function and
       // print its result
       document.write(equalProdPreSuf(arr));
 
 
   // This code is contributed by Potta Lokesh
   </script>


 
 

Output

2

 

Time Complexity: O(N)
Auxiliary Space: O(N)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!