VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-index-to-split-array-into-subarrays-with-co-prime-products/

⇱ Minimum index to split array into subarrays with co-prime products - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum index to split array into subarrays with co-prime products

Last Updated : 23 Jul, 2025

Given an arrayarr[] consisting of N integers, the task is to find the maximum index K such that the product of subarrays{arr[0], arr[K]} and {arr[K + 1], arr[N - 1]} are co-prime. If no such index exists, then print "-1".

Examples:

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.

Naive Approach: The simplest approach is to check all possible indexes of partition from the start of the array and check if the product of the subarrays formed is co-prime or not. If there exists any such index then print that index. Otherwise, print "-1"
Time Complexity: O(N2)
Auxiliary Space: O(1)

Better Approach: To optimize the above approach, the idea is to use the prefix product array and suffix product array and find the possible index. Follow the steps below to solve the problem:

  • Create two auxiliary arrays, prefix[] and suffix[] to store the prefix and suffix array product. Initialize prefix[0] to arr[0] and suffix[N - 1] to arr[N - 1].
  • Traverse the given array over the range [2, N] using variable i and update the prefix array as prefix[i] = prefix[i - 1]*arr[i].
  • Traverse the given array from the back over the range [N - 2, 0] using variable i and update the suffix array as suffix[i] = suffix[i + 1]*arr[i].
  • Iterate a loop over the range [0, N - 1] using variable i and check if prefix[i] and suffix[i + 1] are co-prime or not. If found to be true the print the current index and break out of the loop.
  • If there doesn't exist any such index in the above step then print "-1".

Below is the implementation of the above approach:


Output
2

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

Efficient Approach: Above approach is using a prefix array and suffix array that  stores the product of numbers , if the product of numbers exceeds the integer maximum value then it will lead to overflow , hence a different approach is needed such that it will work for every numbers that are present in the array. Follow the steps below to solve the problem:

  • Create a frequency  map total_freq  that will store the count of  all the prime factors of numbers that are present in the array using the primeFreq function that will calculate prime factors of number
  • Create an another frequency map curr_freq that will store the count of prime factors of current numbers of array. 
  • Iterate over array elements and create a boolean flg with value set to true and store the current prime divisors of number in curr_freq map. Then Iterate over the mapcurr_freq   and check if the current prime factor count in curr_freq map and total_freq map are equal or not. If it is not equal then set flg to false and stop iteration of the curr_freq map and break.
  • After that check if  flg is true  then return the value of current array element's index that is giving this result.
  • If there doesn't exist any such index then print "-1".

Below is the implementation of the above approach:

Output

2

Time Complexity: O(N*sqrt(N))

Auxiliary Space: O(N)

Comment