Skip to content
Related Articles

Related Articles

Smallest index that splits an array into two subarrays with equal product

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 15 Jun, 2021
View Discussion
Improve Article
Save Article

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

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>

Output: 

3

 

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!