Count of indices for which the prefix and suffix product are equal
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)
Better Approach:
This approach to solve the problem is to precompute and store prefix and suffix products in separate arrays. Traversing those arrays simultaneously and checking condition of equality will give our count of indices with equal prefix and suffix product.
Algorithm:
- Initialize a variable res to 0 to store the result.
- Initialize a vector arr to store the given input array.
- Initialize a variable n to store the length of the input array arr.
- Initialize two auxiliary arrays left_Product and right_Product of length n, to store prefix and suffix product at every index.
- Compute the prefix product of arr and store it in left_Product array using a for loop that iterates from 0 to n-1.
- Compute the suffix product of arr and store it in right_Product array using a for loop that iterates from n-2 to 0.
- Traverse the array arr using a for loop that iterates from 0 to n-1.
- For each element arr[i] in arr, if the prefix product left_Product[i] is equal to the suffix product right_Product[i], increment the result variable res by 1.
- Return the result variable res.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int equalProdPreSuf(vector< int >& arr) {
int res = 0;
int n = arr.size();
vector< int > left_Product(n);
vector< int > right_Product(n);
left_Product[0] = arr[0];
for ( int i = 1; i < n; i++) {
left_Product[i] = left_Product[i-1] * arr[i];
}
right_Product[n-1] = arr[n-1];
for ( int i=n - 2; i >= 0; i--) {
right_Product[i] = right_Product[i+1] * arr[i];
}
for ( int i = 0; i < n; i++) {
if (left_Product[i] == right_Product[i]) {
res++;
}
}
return res;
}
int main() {
vector< int > arr = { 4, 5, 1, 1, -2, 5, -2 };
cout << equalProdPreSuf(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int equalProdPreSuf(List<Integer> arr) {
int res = 0 ;
int n = arr.size();
int [] left_Product = new int [n];
int [] right_Product = new int [n];
left_Product[ 0 ] = arr.get( 0 );
for ( int i = 1 ; i < n; i++) {
left_Product[i] = left_Product[i - 1 ] * arr.get(i);
}
right_Product[n - 1 ] = arr.get(n - 1 );
for ( int i = n - 2 ; i >= 0 ; i--) {
right_Product[i] = right_Product[i + 1 ] * arr.get(i);
}
for ( int i = 0 ; i < n; i++) {
if (left_Product[i] == right_Product[i]) {
res++;
}
}
return res;
}
public static void main(String[] args) {
List<Integer> arr = Arrays.asList( 4 , 5 , 1 , 1 , - 2 , 5 , - 2 );
System.out.println(equalProdPreSuf(arr));
}
}
|
Python3
def equal_prod_pre_suf(arr):
res = 0
n = len (arr)
left_product = [ 0 ] * n
right_product = [ 0 ] * n
left_product[ 0 ] = arr[ 0 ]
for i in range ( 1 , n):
left_product[i] = left_product[i - 1 ] * arr[i]
right_product[n - 1 ] = arr[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
right_product[i] = right_product[i + 1 ] * arr[i]
for i in range (n):
if left_product[i] = = right_product[i]:
res + = 1
return res
if __name__ = = "__main__" :
arr = [ 4 , 5 , 1 , 1 , - 2 , 5 , - 2 ]
print (equal_prod_pre_suf(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int EqualProdPreSuf(List< int > arr)
{
int res = 0;
int n = arr.Count;
int [] leftProduct = new int [n];
int [] rightProduct = new int [n];
leftProduct[0] = arr[0];
for ( int i = 1; i < n; i++)
{
leftProduct[i] = leftProduct[i - 1] * arr[i];
}
rightProduct[n - 1] = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
rightProduct[i] = rightProduct[i + 1] * arr[i];
}
for ( int i = 0; i < n; i++)
{
if (leftProduct[i] == rightProduct[i])
{
res++;
}
}
return res;
}
public static void Main( string [] args)
{
List< int > arr = new List< int > { 4, 5, 1, 1, -2, 5, -2 };
Console.WriteLine(EqualProdPreSuf(arr));
}
}
|
Javascript
function equalProdPreSuf(arr) {
let res = 0;
const n = arr.length;
const leftProduct = new Array(n);
const rightProduct = new Array(n);
leftProduct[0] = arr[0];
for (let i = 1; i < n; i++) {
leftProduct[i] = leftProduct[i - 1] * arr[i];
}
rightProduct[n - 1] = arr[n - 1];
for (let i = n - 2; i >= 0; i--) {
rightProduct[i] = rightProduct[i + 1] * arr[i];
}
for (let i = 0; i < n; i++) {
if (leftProduct[i] === rightProduct[i]) {
res++;
}
}
return res;
}
const arr = [4, 5, 1, 1, -2, 5, -2];
console.log(equalProdPreSuf(arr));
|
Time Complexity : O(N), As we iterate the array (arr) thrice. Where N = size of the array.
Auxiliary Space : O(N), array left_Product and right_Product space. Where N = size of the array.
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++
#include <bits/stdc++.h>
using namespace std;
int equalProdPreSuf(vector< int >& arr)
{
int res = 0;
int preProd = 1, sufProd = 1;
int len = arr.size();
vector< int > prod(len, 0);
for ( int i = len - 1; i >= 0; i--) {
sufProd *= arr[i];
prod[i] = sufProd;
}
for ( int i = 0; i < len; i++) {
preProd *= arr[i];
if (preProd == prod[i]) {
res++;
}
}
return res;
}
int main()
{
vector< int > arr = { 4, 5, 1, 1, -2, 5, -2 };
cout << equalProdPreSuf(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int equalProdPreSuf( int [] arr)
{
int res = 0 ;
int preProd = 1 , sufProd = 1 ;
int len = arr.length;
int [] prod = new int [len];
for ( int i = len - 1 ; i >= 0 ; i--) {
sufProd *= arr[i];
prod[i] = sufProd;
}
for ( int i = 0 ; i < len; i++) {
preProd *= arr[i];
if (preProd == prod[i]) {
res++;
}
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 4 , 5 , 1 , 1 , - 2 , 5 , - 2 };
System.out.println(equalProdPreSuf(arr));
}
}
|
Python3
def equalProdPreSuf(arr):
res = 0
preProd = 1
sufProd = 1
Len = len (arr)
prod = [ 0 ] * Len
for i in range ( Len - 1 , 0 , - 1 ):
sufProd * = arr[i]
prod[i] = sufProd
for i in range ( Len ):
preProd * = arr[i]
if (preProd = = prod[i]):
res + = 1
return res
arr = [ 4 , 5 , 1 , 1 , - 2 , 5 , - 2 ]
print (equalProdPreSuf(arr))
|
C#
using System;
class GFG {
public static int equalProdPreSuf( int [] arr)
{
int res = 0;
int preProd = 1, sufProd = 1;
int len = arr.Length;
int [] prod = new int [len];
for ( int i = len - 1; i >= 0; i--) {
sufProd *= arr[i];
prod[i] = sufProd;
}
for ( int i = 0; i < len; i++) {
preProd *= arr[i];
if (preProd == prod[i]) {
res++;
}
}
return res;
}
public static void Main(String[] args)
{
int [] arr = { 4, 5, 1, 1, -2, 5, -2 };
Console.Write(equalProdPreSuf(arr));
}
}
|
Javascript
<script>
function equalProdPreSuf(arr) {
let res = 0;
let preProd = 1, sufProd = 1;
let len = arr.length;
let prod = new Array(len).fill(0);
for (let i = len - 1; i >= 0; i--) {
sufProd *= arr[i];
prod[i] = sufProd;
}
for (let i = 0; i < len; i++) {
preProd *= arr[i];
if (preProd == prod[i]) {
res++;
}
}
return res;
}
let arr = [4, 5, 1, 1, -2, 5, -2];
document.write(equalProdPreSuf(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...