给定一个由N个整数组成的数组arr [] ,任务是找到最大索引K ,以使子数组{arr [0],arr [K]}和{arr [K + 1],arr [N – 1]}是互质的。如果不存在这样的索引,则打印“ -1” 。
例子:
Input: arr[] = {2, 3, 4, 5}
Output: 2
Explanation:
Smallest index for partition is 2.
Product of left subarray is = 2 * 3 * 4 = 24.
Product of right subarray = 5.
Since 24 and 5 are co-prime, the required answer is 2.
Input: arr[] = {23, 41, 52, 83, 7, 13}
Output: 0
Explanation:
Smallest index for partition is 0.
Product of left subarray = 23.
Product of right subarray = 41 * 52 * 83 * 7 * 13 = 16102996.
Since 23 and 16102996 are co-prime, the answer is 0.
天真的方法:最简单的方法是从数组的开头检查所有可能的分区索引,并检查形成的子数组的乘积是否是互素的。如果存在任何这样的索引,则打印该索引。否则,打印“ -1” 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用前缀乘积数组和后缀乘积数组并查找可能的索引。请按照以下步骤解决问题:
- 创建两个辅助数组prefix []和suffix [],以存储前缀和后缀数组乘积。将前缀[0]初始化为arr [0],并将后缀[N – 1]初始化为arr [N – 1] 。
- 使用变量i遍历[2,N]范围内的给定数组,并将前缀数组更新为prefix [i] = prefix [i – 1] * arr [i] 。
- 使用变量i从背面遍历[N – 2,0]范围内的给定数组,并将后缀数组更新为suffix [i] = suffix [i + 1] * arr [i] 。
- 使用变量i遍历[0,N – 1]范围内的循环,并检查前缀[i]和后缀[i + 1]是否互质。如果发现为真,则打印当前索引并退出循环。
- 如果在上面的步骤中不存在任何这样的索引,则打印“ -1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the GCD of 2 numbers
int GCD(int a, int b)
{
// Base Case
if (b == 0)
return a;
// Find the GCD recursively
return GCD(b, a % b);
}
// Function to find the minimum partition
// index K s.t. product of both subarrays
// around that partition are co-prime
int findPartition(int nums[], int N)
{
// Stores the prefix and suffix
// array product
int prefix[N], suffix[N], i, k;
prefix[0] = nums[0];
// Update the prefix array
for (i = 1; i < N; i++) {
prefix[i] = prefix[i - 1]
* nums[i];
}
suffix[N - 1] = nums[N - 1];
// Update the suffix array
for (i = N - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1]
* nums[i];
}
// Iterate the given array
for (k = 0; k < N - 1; k++) {
// Check if prefix[k] and
// suffix[k+1] are co-prime
if (GCD(prefix[k],
suffix[k + 1])
== 1) {
return k;
}
}
// If no index for partition
// exists, then return -1
return -1;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << findPartition(arr, N);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class solution{
// Function to find the
// GCD of 2 numbers
static int GCD(int a,
int b)
{
// Base Case
if (b == 0)
return a;
// Find the GCD
// recursively
return GCD(b, a % b);
}
// Function to find the minimum
// partition index K s.t. product
// of both subarrays around that
// partition are co-prime
static int findPartition(int nums[],
int N)
{
// Stores the prefix and
// suffix array product
int []prefix = new int[N];
int []suffix = new int[N];
int i, k;
prefix[0] = nums[0];
// Update the prefix array
for (i = 1; i < N; i++)
{
prefix[i] = prefix[i - 1] *
nums[i];
}
suffix[N - 1] = nums[N - 1];
// Update the suffix array
for (i = N - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] *
nums[i];
}
// Iterate the given array
for (k = 0; k < N - 1; k++)
{
// Check if prefix[k] and
// suffix[k+1] are co-prime
if (GCD(prefix[k],
suffix[k + 1]) == 1)
{
return k;
}
}
// If no index for partition
// exists, then return -1
return -1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {2, 3, 4, 5};
int N = arr.length;
// Function call
System.out.println(findPartition(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 program for the
# above approach
# Function to find the
# GCD of 2 numbers
def GCD(a, b):
# Base Case
if (b == 0):
return a
# Find the GCD recursively
return GCD(b, a % b)
# Function to find the minimum
# partition index K s.t. product
# of both subarrays around that
# partition are co-prime
def findPartition(nums, N):
#Stores the prefix and
# suffix array product
prefix=[0] * N
suffix=[0] * N
prefix[0] = nums[0]
# Update the prefix
# array
for i in range(1, N):
prefix[i] = (prefix[i - 1] *
nums[i])
suffix[N - 1] = nums[N - 1]
# Update the suffix array
for i in range(N - 2, -1, -1):
suffix[i] = (suffix[i + 1] *
nums[i])
# Iterate the given array
for k in range(N - 1):
# Check if prefix[k] and
# suffix[k+1] are co-prime
if (GCD(prefix[k],
suffix[k + 1]) == 1):
return k
# If no index for partition
# exists, then return -1
return -1
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 4, 5]
N = len(arr)
# Function call
print(findPartition(arr, N))
# This code is contributed by Mohit Kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to find the
// GCD of 2 numbers
static int GCD(int a, int b)
{
// Base Case
if (b == 0)
return a;
// Find the GCD
// recursively
return GCD(b, a % b);
}
// Function to find the minimum
// partition index K s.t. product
// of both subarrays around that
// partition are co-prime
static int findPartition(int[] nums,
int N)
{
// Stores the prefix and
// suffix array product
int[] prefix = new int[N];
int[] suffix = new int[N];
int i, k;
prefix[0] = nums[0];
// Update the prefix array
for (i = 1; i < N; i++)
{
prefix[i] = prefix[i - 1] *
nums[i];
}
suffix[N - 1] = nums[N - 1];
// Update the suffix array
for (i = N - 2; i >= 0; i--)
{
suffix[i] = suffix[i + 1] *
nums[i];
}
// Iterate the given array
for (k = 0; k < N - 1; k++)
{
// Check if prefix[k] and
// suffix[k+1] are co-prime
if (GCD(prefix[k],
suffix[k + 1]) == 1)
{
return k;
}
}
// If no index for partition
// exists, then return -1
return -1;
}
// Driver code
static void Main()
{
int[] arr = {2, 3, 4, 5};
int N = arr.Length;
// Function call
Console.WriteLine(findPartition(arr, N));
}
}
// This code is contributed by divyeshrabadiya07
2
时间复杂度: O(N log(N))
辅助空间: O(N)