Given an array(1-based indexing) arr[] consisting of N non zero integers, the task is to find the leftmost index i such that the product of all the elements of the subarrays arr[1, i] and arr[i + 1, N] is the same.
Examples:
Input: arr[] = {1, 2, 3, 3, 2, 1}
Output: 3
Explanation: Index 3 generates subarray {arr[1], arr[3]} with product 6 (= 1 * 2 * 3) and {arr[4], arr[6]} with product 6 ( = 3 * 2 * 1).
Input: arr = {3, 2, 6}
Output: 2
Brute Force Approach:
To solve this problem is to iterate through the array and keep track of the product of the elements from the beginning of the array to the current index and from the current index to the end of the array. For each index i, if the product of the two subarrays is equal, return i. If no such index is found, return -1.
Implementation of the above approach:
C++
#include <iostream>
using namespace std;
void prodEquilibrium( int arr[], int N)
{
int leftProduct = 1;
int rightProduct = 1;
for ( int i = 0; i < N; i++) {
leftProduct *= arr[i];
for ( int j = i + 1; j < N; j++) {
rightProduct *= arr[j];
}
if (leftProduct == rightProduct) {
cout << i + 1 << endl;
return ;
}
rightProduct = 1;
}
cout << -1 << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
prodEquilibrium(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void prodEquilibrium( int [] arr, int N)
{
int leftProduct = 1 ;
int rightProduct = 1 ;
for ( int i = 0 ; i < N; i++) {
leftProduct *= arr[i];
for ( int j = i + 1 ; j < N; j++) {
rightProduct *= arr[j];
}
if (leftProduct == rightProduct) {
System.out.println(i + 1 );
return ;
}
rightProduct = 1 ;
}
System.out.println(- 1 );
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 3 , 2 , 1 };
int N = arr.length;
prodEquilibrium(arr, N);
}
}
|
Python3
def prodEquilibrium(arr, N):
leftProduct = 1
rightProduct = 1
for i in range (N):
leftProduct * = arr[i]
for j in range (i + 1 , N):
rightProduct * = arr[j]
if leftProduct = = rightProduct:
print (i + 1 )
return
rightProduct = 1
print ( - 1 )
arr = [ 1 , 2 , 3 , 3 , 2 , 1 ]
N = len (arr)
prodEquilibrium(arr, N)
|
C#
using System;
public class Program
{
public static void ProdEquilibrium( int [] arr, int N)
{
int leftProduct = 1;
int rightProduct = 1;
for ( int i = 0; i < N; i++)
{
leftProduct *= arr[i];
for ( int j = i + 1; j < N; j++)
{
rightProduct *= arr[j];
}
if (leftProduct == rightProduct)
{
Console.WriteLine(i + 1);
return ;
}
rightProduct = 1;
}
Console.WriteLine( "-1" );
}
public static void Main()
{
int [] arr = { 1, 2, 3, 3, 2, 1 };
int N = arr.Length;
ProdEquilibrium(arr, N);
}
}
|
Javascript
function prodEquilibrium(arr, N) {
let leftProduct = 1;
let rightProduct = 1;
for (let i = 0; i < N; i++) {
leftProduct *= arr[i];
for (let j = i + 1; j < N; j++) {
rightProduct *= arr[j];
}
if (leftProduct === rightProduct) {
console.log(i + 1);
return ;
}
rightProduct = 1;
}
console.log(-1);
}
let arr = [1, 2, 3, 3, 2, 1];
let N = arr.length;
prodEquilibrium(arr, N);
|
Time Complexity: O(N^2)
Space Complexity: O(1)
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say product, > that stores the product of all the array elements.
- Traverse the given array and find the product of all the array elements store it in the product.
- Initialize two variables left and right to 1 that stores the product of the left and the right subarray
- Traverse the given array and perform the following steps:
- Multiply the value of left by arr[i].
- Divide the value of right by arr[i].
- If the value of left is equal to right, then print the value of the current index i as the resultant index and break out of the loop.
- After completing the above steps, if any such index doesn’t exist, then print “-1” as the result.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void prodEquilibrium( int arr[], int N)
{
int product = 1;
for ( int i = 0; i < N; i++) {
product *= arr[i];
}
int left = 1;
int right = product;
for ( int i = 0; i < N; i++) {
left = left * arr[i];
right = right / arr[i];
if (left == right) {
cout << i + 1 << endl;
return ;
}
}
cout << -1 << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
prodEquilibrium(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void prodEquilibrium( int arr[], int N)
{
int product = 1 ;
for ( int i = 0 ; i < N; i++)
{
product *= arr[i];
}
int left = 1 ;
int right = product;
for ( int i = 0 ; i < N; i++)
{
left = left * arr[i];
right = right / arr[i];
if (left == right)
{
System.out.print(i + 1 + "\n" );
return ;
}
}
System.out.print(- 1 + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 3 , 2 , 1 };
int N = arr.length;
prodEquilibrium(arr, N);
}
}
|
Python3
def prodEquilibrium(arr, N):
product = 1
for i in range (N):
product * = arr[i]
left = 1
right = product
for i in range (N):
left = left * arr[i]
right = right / / arr[i]
if (left = = right):
print (i + 1 )
return
print ( - 1 )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 3 , 2 , 1 ]
N = len (arr)
prodEquilibrium(arr, N)
|
C#
using System;
class GFG {
static void prodEquilibrium( int [] arr, int N)
{
int product = 1;
for ( int i = 0; i < N; i++) {
product *= arr[i];
}
int left = 1;
int right = product;
for ( int i = 0; i < N; i++) {
left = left * arr[i];
right = right / arr[i];
if (left == right) {
Console.WriteLine(i + 1 + "\n" );
return ;
}
}
Console.WriteLine(-1 + "\n" );
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 3, 2, 1 };
int N = arr.Length;
prodEquilibrium(arr, N);
}
}
|
Javascript
<script>
function prodEquilibrium(arr, N)
{
let product = 1;
for (let i = 0; i < N; i++) {
product *= arr[i];
}
let left = 1;
let right = product;
for (let i = 0; i < N; i++) {
left = left * arr[i];
right = right / arr[i];
if (left == right) {
document.write((i + 1) + "<br>" );
return ;
}
}
document.write((-1) + "<br>" );
}
let arr = [ 1, 2, 3, 3, 2, 1 ];
let N = arr.length;
prodEquilibrium(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Apr, 2023
Like Article
Save Article