Smallest index that splits an array into two subarrays with equal product
Last Updated :
11 Apr, 2023
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;
// Function to find the smallest index that splits the array
// into two subarrays with equal product
void prodEquilibrium(int arr[], int N)
{
int leftProduct = 1;
int rightProduct = 1;
for (int i = 0; i < N; i++) {
// calculate product of elements from 0 to i
leftProduct *= arr[i];
// calculate product of elements from i+1 to N-1
for (int j = i + 1; j < N; j++) {
rightProduct *= arr[j];
}
// if the product of the two subarrays is equal,
// return i
if (leftProduct == rightProduct) {
cout << i + 1 << endl;
return;
}
// reset rightProduct for the next iteration
rightProduct = 1;
}
// if no such index is found, return -1
cout << -1 << endl;
}
// Driver Code
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 {
// Function to find the smallest index that splits the
// array into two subarrays with equal product
static void prodEquilibrium(int[] arr, int N)
{
int leftProduct = 1;
int rightProduct = 1;
for (int i = 0; i < N; i++) {
// calculate product of elements from 0 to i
leftProduct *= arr[i];
// calculate product of elements from i+1 to N-1
for (int j = i + 1; j < N; j++) {
rightProduct *= arr[j];
}
// if the product of the two subarrays is equal,
// return i
if (leftProduct == rightProduct) {
System.out.println(i + 1);
return;
}
// reset rightProduct for the next iteration
rightProduct = 1;
}
// if no such index is found, return -1
System.out.println(-1);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int N = arr.length;
prodEquilibrium(arr, N);
}
}
// This code is contributed by Prajwal Kandekar
Python3
def prodEquilibrium(arr, N):
leftProduct = 1
rightProduct = 1
for i in range(N):
# calculate product of elements from 0 to i
leftProduct *= arr[i]
# calculate product of elements from i+1 to N-1
for j in range(i+1, N):
rightProduct *= arr[j]
# if the product of the two subarrays is equal, return i
if leftProduct == rightProduct:
print(i+1)
return
# reset rightProduct for the next iteration
rightProduct = 1
# if no such index is found, return -1
print(-1)
# Driver Code
arr = [1, 2, 3, 3, 2, 1]
N = len(arr)
prodEquilibrium(arr, N)
# This code is contributed by Prajwal Kandekar
C#
using System;
public class Program
{
// Function to find the smallest index that splits the array
// into two subarrays with equal product
public static void ProdEquilibrium(int[] arr, int N)
{
int leftProduct = 1;
int rightProduct = 1;
for (int i = 0; i < N; i++)
{
// calculate product of elements from 0 to i
leftProduct *= arr[i];
// calculate product of elements from i+1 to N-1
for (int j = i + 1; j < N; j++)
{
rightProduct *= arr[j];
}
// if the product of the two subarrays is equal,
// return i
if (leftProduct == rightProduct)
{
Console.WriteLine(i + 1);
return;
}
// reset rightProduct for the next iteration
rightProduct = 1;
}
// if no such index is found, return -1
Console.WriteLine("-1");
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int N = arr.Length;
ProdEquilibrium(arr, N);
}
}
JavaScript
// Javascript code for the above approach
// Function to find the smallest index that splits the array into
// two subarrays with equal product
function prodEquilibrium(arr, N) {
let leftProduct = 1;
let rightProduct = 1;
for (let i = 0; i < N; i++) {
// calculate product of elements from 0 to i
leftProduct *= arr[i];
// calculate product of elements from i+1 to N-1
for (let j = i + 1; j < N; j++) {
rightProduct *= arr[j];
}
// if the product of the two subarrays is equal, return i
if (leftProduct === rightProduct) {
console.log(i + 1);
return;
}
// reset rightProduct for the next iteration
rightProduct = 1;
}
// if no such index is found, return -1
console.log(-1);
}
// Driver Code
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++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the smallest
// index that splits the array into
// two subarrays with equal product
void prodEquilibrium(int arr[], int N)
{
// Stores the product of the array
int product = 1;
// Traverse the given array
for (int i = 0; i < N; i++) {
product *= arr[i];
}
// Stores the product of left
// and the right subarrays
int left = 1;
int right = product;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update the products
left = left * arr[i];
right = right / arr[i];
// Check if product is equal
if (left == right) {
// Print resultant index
cout << i + 1 << endl;
return;
}
}
// If no partition exists, then
// print -1.
cout << -1 << endl;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
prodEquilibrium(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the smallest
// index that splits the array into
// two subarrays with equal product
static void prodEquilibrium(int arr[], int N)
{
// Stores the product of the array
int product = 1;
// Traverse the given array
for(int i = 0; i < N; i++)
{
product *= arr[i];
}
// Stores the product of left
// and the right subarrays
int left = 1;
int right = product;
// Traverse the given array
for(int i = 0; i < N; i++)
{
// Update the products
left = left * arr[i];
right = right / arr[i];
// Check if product is equal
if (left == right)
{
// Print resultant index
System.out.print(i + 1 + "\n");
return;
}
}
// If no partition exists, then
// print -1.
System.out.print(-1 + "\n");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 3, 2, 1 };
int N = arr.length;
prodEquilibrium(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
# Function to find the smallest
# index that splits the array into
# two subarrays with equal product
def prodEquilibrium(arr, N):
# Stores the product of the array
product = 1
# Traverse the given array
for i in range(N):
product *= arr[i]
# Stores the product of left
# and the right subarrays
left = 1
right = product
# Traverse the given array
for i in range(N):
# Update the products
left = left * arr[i]
right = right // arr[i]
# Check if product is equal
if (left == right):
# Print resultant index
print(i + 1)
return
# If no partition exists, then
# print -1.
print(-1)
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 3, 2, 1]
N = len(arr)
prodEquilibrium(arr, N)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the smallest
// index that splits the array into
// two subarrays with equal product
static void prodEquilibrium(int[] arr, int N)
{
// Stores the product of the array
int product = 1;
// Traverse the given array
for (int i = 0; i < N; i++) {
product *= arr[i];
}
// Stores the product of left
// and the right subarrays
int left = 1;
int right = product;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update the products
left = left * arr[i];
right = right / arr[i];
// Check if product is equal
if (left == right) {
// Print resultant index
Console.WriteLine(i + 1 + "\n");
return;
}
}
// If no partition exists, then
// print -1.
Console.WriteLine(-1 + "\n");
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int N = arr.Length;
prodEquilibrium(arr, N);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the smallest
// index that splits the array into
// two subarrays with equal product
function prodEquilibrium(arr, N)
{
// Stores the product of the array
let product = 1;
// Traverse the given array
for (let i = 0; i < N; i++) {
product *= arr[i];
}
// Stores the product of left
// and the right subarrays
let left = 1;
let right = product;
// Traverse the given array
for (let i = 0; i < N; i++) {
// Update the products
left = left * arr[i];
right = right / arr[i];
// Check if product is equal
if (left == right) {
// Print resultant index
document.write((i + 1) + "<br>");
return;
}
}
// If no partition exists, then
// print -1.
document.write((-1) + "<br>");
}
// Driver Code
let arr = [ 1, 2, 3, 3, 2, 1 ];
let N = arr.length;
prodEquilibrium(arr, N);
// This code is contributed by Dharanendra L V.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum index to split array into subarrays with co-prime products Given an array arr[] consisting of N integers, the task is to find the maximum index K such that the product of subarrays {arr[0], arr[K]} and {arr[K + 1], arr[N - 1]} are co-prime. If no such index exists, then print "-1". Examples: Input: arr[] = {2, 3, 4, 5}Output: 2Explanation:Smallest index for
15 min read
Find an element which divides the array in two subarrays with equal product Given, an array of size N. Find an element which divides the array into two sub-arrays with equal product. Print -1 if no such partition is not possible. Examples : Input : 1 4 2 1 4 Output : 2 If 2 is the partition, subarrays are : {1, 4} and {1, 4} Input : 2, 3, 4, 1, 4, 6 Output : 1 If 1 is the p
12 min read
Split an array into two equal Sum subarrays Given an array of integers greater than zero, find if it is possible to split it in two subarrays (without reordering the elements), such that the sum of the two subarrays is the same. Print the two subarrays. Examples : Input : Arr[] = { 1 , 2 , 3 , 4 , 5 , 5 } Output : { 1 2 3 4 } { 5 , 5 } Input
15 min read
Find the longest Subarray with equal Sum and Product Given an array arr[] of N integers, the task is to find the length of the longest subarray where the sum of the elements in the subarray is equal to the product of the elements in the subarray. Examples: Input: arr[] = [1, 2, 1, 2, 2, 5, 6, 24]Output: 5Explanation: The subarray [1, 2, 1, 2, 2] has a
11 min read
Split array into three continuous subarrays with negative, 0 and positive product respectively Given an array arr[] size N such that each array element is either -1, 0, or 1, the task is to check if is it possible to split the array into 3 contiguous subarrays such that the product of the first, second and third subarrays is negative, 0 and positive respectively. Examples: Input: arr[] = {-1,
10 min read
Smallest subarray with product divisible by k Given an array of non-negative numbers, find length of smallest subarray whose product is a multiple of k. Examples : Input : arr[] = {1, 9, 16, 5, 4, 3, 2} k = 720 Output: 3 The smallest subarray is {9, 16, 5} whose product is 720. Input : arr[] = {1, 2, 4, 5, 6} K = 96 Output : No such subarray ex
7 min read
Number of subarrays for which product and sum are equal Given a array of n numbers. We need to count the number of subarrays having the product and sum of elements are equal Examples: Input : arr[] = {1, 3, 2} Output : 4 The subarrays are : [0, 0] sum = 1, product = 1, [1, 1] sum = 3, product = 3, [2, 2] sum = 2, product = 2 and [0, 2] sum = 1+3+2=6, pro
5 min read
Smallest subarray which upon repetition gives the original array Given an array arr[] of N integers, the task is to find the smallest subarray brr[] of size at least 2 such that by performing repeating operation on the array brr[] gives the original array arr[]. Print "-1" if it is not possible to find such a subarray. A repeating operation on an array is to appe
8 min read
Smallest array that can be obtained by replacing adjacent pairs with their products Given an array arr[] of size N, the task is to print the least possible size the given array can be reduced to by performing the following operations: Remove any two adjacent elements, say arr[i] and arr[i+1] and insert a single element arr[i] * arr[i+1] at that position in the array.If all array el
4 min read
Smallest subarray whose product leaves remainder K when divided by size of the array Given an array arr[] of N integers and an integer K, the task is to find the length of the smallest subarray whose product when divided by N gives remainder K. If no such subarray exists the print "-1". Examples: Input: N = 3, arr = {2, 2, 6}, K = 1 Output: 2 Explanation: All possible subarrays are:
6 min read