- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find an element which divides the array in two subarrays with equal product in Python
Suppose we have an array of size N; we have to find an element which divides the array into two different sub-arrays with equal product. Return -1 if no such partition is possible.
So, if the input is like [2,5,3,2,5], then the output will be 3 then subarrays are: {2, 5} and {2, 5}
To solve this, we will follow these steps −
- n := size of array
- multiply_pref := a new list
- insert array[0] at the end of multiply_pref
- for i in range 1 to n, do
- insert multiply_pref[i-1]*array[i] at the end of multiply_pref
- multiply_suff := a list of size n, and fill with none
- multiply_suff[n-1] := array[n-1]
- for i in range n-2 to -1, decrease by 1, do
- multiply_suff[i] := multiply_suff[i+1]*array[i]
- for i in range 1 to n-1, do
- if multiply_pref[i] is same as multiply_suff[i], then
- return array[i]
- if multiply_pref[i] is same as multiply_suff[i], then
- return -1
Example Code
Let us see the following implementation to get better understanding −
def search_elem(array): n = len(array) multiply_pref = [] multiply_pref.append(array[0]) for i in range(1, n): multiply_pref.append(multiply_pref[i-1]*array[i]) multiply_suff = [None for i in range(0, n)] multiply_suff[n-1] = array[n-1] for i in range(n-2, -1, -1): multiply_suff[i] = multiply_suff[i+1]*array[i] for i in range(1, n-1): if multiply_pref[i] == multiply_suff[i]: return array[i] return -1 array = [2,5,3,2,5] print(search_elem(array))
Input
[2,5,3,2,5]
Output
3
- Related Articles
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- Check if the array has an element which is equal to product of remaining elements in Python
- Find if array can be divided into two subarrays of equal sum in C++
- Program to find maximum product of two distinct elements from an array in Python
- Check if the array has an element which is equal to sum of all the remaining elements in Python
- Program to find maximum XOR with an element from array in Python
- Count subarrays with equal number of occurrences of two given elements in C++
- Python Program to find the largest element in an array
- Find the least frequent element in an array using Python
- Finding n subarrays with equal sum in JavaScript
- Python Program to find largest element in an array
- Smallest integer > 1 which divides every element of the given array: Using C++
- Subarrays product sum in JavaScript
- Check if it possible to partition in k subarrays with equal sum in Python
- Check if subarray with given product exists in an array in Python

Advertisements