📌  相关文章
📜  将数组拆分为具有互质乘积的子数组的最小索引

📅  最后修改于: 2021-10-26 06:34:58             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到最大索引K ,使得子数组{arr[0], arr[K]}{arr[K + 1], arr[N – 1]}是互质的。如果不存在这样的索引,则打印“-1”

例子:

朴素的方法:最简单的方法是从数组的开头检查所有可能的分区索引,并检查形成的子数组的乘积是否互质。如果存在任何此类索引,则打印该索引。否则,打印“-1”
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:对上述方法进行优化,思路是使用前缀乘积数组和后缀乘积数组,找到可能的索引。请按照以下步骤解决问题:

  • 创建两个辅助数组prefix[]suffix[]来存储前缀和后缀数组的乘积。将prefix[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] 上迭代一个循环并检查prefix[i]suffix[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


Javascript


输出:
2

时间复杂度: O(N log(N))
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程