Given an array arr[] of length N, the task is to find the smallest pair of indices (i, j) such that the product of elements in the subarray arr[i + 1, j – 1] is co-prime with either the product of the subarray arr[0, i] or that of the subarray arr[j, N]. If no such pair exists, the print “-1”.
Examples:
Input: arr[] = {2, 4, 1, 3, 7}
Output: (0, 2)
Explanation: The product of the subarray {arr[1], arr[1]} is 4. The product of right subarray = 1 * 3 * 7 = 21. Since 4 and 21 are co-primes, then print the range (0, 2) as the answer.
Input: arr[] = {21, 3, 11, 7, 18}
Output: (1, 3)
Explanation: The product of the subarray {arr[1], arr[2]} is 11. The product of right subarray is 7 * 18 = 126. Since 11 and 126 are co-primes, then print the range (1, 3) as the answer.
Naive Approach: The simplest approach is to iterate through every possible pair of indices (i, j) and for each pair, find the product of subarray arr[0, i] and arr[j, N] and check if it is co-prime with the product of the subarray arr[i + 1, j – 1] or not. If found to be true, then print those pair of indices. If no such pair exists, then print “-1”.
Time Complexity: O((N3)*log(M)), where M is the product of all elements of the array.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use an auxiliary array to store the suffix product of the array elements. Follow the steps below to solve the problem:
- Store the product of suffix elements in rightProd[], where rightProd[i] stores the product of elements from arr[i], arr[N – 1].
- Find the product of all elements in the array as totalProd.
- Traverse the given array using the variable i and perform the following operations:
- Initialize a variable, say product.
- Iterate through the array using variable j from the range [i, N – 1].
- Update product by multiplying product by arr[j].
- Initialize leftProduct as total/right_prod[i].
- Check if the product is co-prime with one of the leftProduct or rightProduct or not. If found to be true, then print the pair (i – 1, j + 1) and break out of the loop.
- After the above steps, if no such pair exists then print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void findPair( int A[], int N)
{
int right_prod[N];
int flag = 0;
right_prod[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--)
right_prod[i] = right_prod[i + 1] * A[i];
int total_prod = right_prod[0];
int product;
for ( int i = 1; i < N - 1; i++) {
product = 1;
for ( int j = i; j < N - 1; j++) {
product *= A[j];
if (gcd(product, right_prod[j + 1]) == 1
|| gcd(product, total_prod / right_prod[i]) == 1) {
flag = 1;
cout << "(" << i - 1 << ", " << j + 1 << ")" ;
break ;
}
}
if (flag == 1)
break ;
}
if (flag == 0)
cout << -1;
}
int main()
{
int arr[] = { 2, 4, 1, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findPair(arr, N);
return 0;
}
|
C
#include <stdio.h>
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void findPair( int A[], int N)
{
int right_prod[N];
int flag = 0;
right_prod[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--)
right_prod[i] = right_prod[i + 1] * A[i];
int total_prod = right_prod[0];
int product;
for ( int i = 1; i < N - 1; i++) {
product = 1;
for ( int j = i; j < N - 1; j++) {
product *= A[j];
if (gcd(product, right_prod[j + 1]) == 1
|| gcd(product, total_prod / right_prod[i]) == 1) {
flag = 1;
printf ( "(%d, %d)" , i - 1, j + 1);
break ;
}
}
if (flag == 1)
break ;
}
if (flag == 0)
printf ( "-1" );
}
int main()
{
int arr[] = { 2, 4, 1, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findPair(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void findPair( int A[], int N)
{
int right_prod[] = new int [N];
int flag = 0 ;
right_prod[N - 1 ] = A[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--)
right_prod[i] = right_prod[i + 1 ] * A[i];
int total_prod = right_prod[ 0 ];
int product;
for ( int i = 1 ; i < N - 1 ; i++)
{
product = 1 ;
for ( int j = i; j < N - 1 ; j++)
{
product *= A[j];
if (gcd(product, right_prod[j + 1 ]) == 1 ||
gcd(product, total_prod /
right_prod[i]) == 1 )
{
flag = 1 ;
System.out.println( "(" + (i - 1 ) + ", " +
(j + 1 ) + ")" );
break ;
}
}
if (flag == 1 )
break ;
}
if (flag == 0 )
System.out.print(- 1 );
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 1 , 3 , 7 };
int N = arr.length;
findPair(arr, N);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def findPair(A, N):
right_prod = [ 0 ] * N
flag = 0
right_prod[N - 1 ] = A[N - 1 ]
for i in range (N - 2 , 0 , - 1 ):
right_prod[i] = right_prod[i + 1 ] * A[i]
total_prod = right_prod[ 0 ]
product = 1
for i in range ( 1 , N - 1 ):
product = 1
for j in range (i, N - 1 ):
product * = A[j]
if (gcd(product, right_prod[j + 1 ]) = = 1 or
gcd(product, total_prod /
right_prod[i]) = = 1 ):
flag = 1
print ( "(" , (i - 1 ) , ", " ,
(j + 1 ) , ")" )
break
if (flag = = 1 ):
break
if (flag = = 0 ):
print ( - 1 )
if __name__ = = '__main__' :
arr = [ 2 , 4 , 1 , 3 , 7 ]
N = len (arr)
findPair(arr, N)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void findPair( int []A, int N)
{
int []right_prod = new int [N];
int flag = 0;
right_prod[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--)
right_prod[i] = right_prod[i + 1] * A[i];
int total_prod = right_prod[0];
int product;
for ( int i = 1; i < N - 1; i++)
{
product = 1;
for ( int j = i; j < N - 1; j++)
{
product *= A[j];
if (gcd(product, right_prod[j + 1]) == 1 ||
gcd(product, total_prod /
right_prod[i]) == 1)
{
flag = 1;
Console.WriteLine( "(" + (i - 1) + ", " +
(j + 1) + ")" );
break ;
}
}
if (flag == 1)
break ;
}
if (flag == 0)
Console.Write(-1);
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 1, 3, 7 };
int N = arr.Length;
findPair(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function findPair(A, N)
{
let right_prod = [];
let flag = 0;
right_prod[N - 1] = A[N - 1];
for (let i = N - 2; i >= 0; i--)
right_prod[i] = right_prod[i + 1] * A[i];
let total_prod = right_prod[0];
let product;
for (let i = 1; i < N - 1; i++)
{
product = 1;
for (let j = i; j < N - 1; j++)
{
product *= A[j];
if (gcd(product, right_prod[j + 1]) == 1 ||
gcd(product, total_prod /
right_prod[i]) == 1)
{
flag = 1;
document.write( "(" + (i - 1) + ", " +
(j + 1) + ")" );
break ;
}
}
if (flag == 1)
break ;
}
if (flag == 0)
document.write(-1);
}
let arr = [ 2, 4, 1, 3, 7 ];
let N = arr.length;
findPair(arr, N);
</script>
|
Time Complexity: O(N2*log(M)), where M is the product of all elements in the array
Auxiliary Space: O(N)