给定一个由N 个非零整数组成的数组(基于 1 的索引) arr[] ,任务是找到最左边的索引i ,使得子数组 arr[1, i]和arr[i + 1,N]是一样的。
例子:
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
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如product , > ,它存储所有数组元素的乘积。
- 遍历给定的数组并找到所有数组元素的乘积,将其存储在product 中。
- 将left和right两个变量初始化为1 ,用于存储左右子数组的乘积
- 遍历给定数组并执行以下步骤:
- 将left的值乘以arr[i] 。
- 将right的值除以arr[i] 。
- 如果left的值等于right ,则将当前索引i的值打印为结果索引并跳出循环。
- 完成上述步骤后,如果任何此类索引不存在,则打印“-1”作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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.
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live