Minimize Array length by repeatedly replacing co-prime pairs with 1
Last Updated :
07 May, 2021
Given an array arr[] consisting of N elements, the task is to minimize the array length by replacing any two coprime array elements with 1.
Examples:
Input: arr[] = {2, 3, 5}
Output: 1
Explanation:
Replace {2, 3} with 1 modifies the array to {1, 5}.
Replace {1, 5} with 1 modifies the array to {1}.
Input: arr[] = {6, 9, 15}
Output: 3
Explanation: No coprime pairs exist in the array. Therefore, no reduction possible.
Naive Approach: The simplest approach is to iterate over the array and check for coprime pairs. If found replace it with 1 search for the next coprime pair and so on.
Time Complexity: O(N3 * log N)
Auxiliary Space: O(1)
Efficient Approach: This approach is based on the fact:
1 is coprime with every number
The idea is to find if there is any co-prime pair present in the array or not. If found, then all the array elements can be reduced to 1 based on the above fact. Hence, if any co-prime pair is found, then, the required answer will be 1, else, the answer will be the initial size of the array.
Illustration:
For arr[] = {2, 4, 6, 8, 9}
Here, as there exists a coprime pair {2, 9}, replacing them by 1 modifies the array to {1, 4, 6, 8}.
Since 1 is coprime with every number, the array can be reduced further in following steps:
{1, 4, 6, 8} -> {1, 6, 8} -> {1, 8} -> {1}
Hence, the array can be reduced to size 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool hasCoprimePair(vector< int >& arr, int n)
{
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if (__gcd(arr[i], arr[j]) == 1) {
return true ;
}
}
}
return false ;
}
int main()
{
int n = 3;
vector< int > arr = { 6, 9, 15 };
if (hasCoprimePair(arr, n)) {
cout << 1 << endl;
}
else {
cout << n << endl;
}
}
|
Java
import java.util.*;
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
static boolean hasCoprimePair( int []arr, int n)
{
for ( int i = 0 ; i < n - 1 ; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
if ((__gcd(arr[i], arr[j])) == 1 )
{
return true ;
}
}
}
return false ;
}
public static void main(String[] args)
{
int n = 3 ;
int []arr = { 6 , 9 , 15 };
if (hasCoprimePair(arr, n))
{
System.out.print( 1 + "\n" );
}
else
{
System.out.print(n + "\n" );
}
}
}
|
Python3
import math
def hasCoprimePair(arr, n):
for i in range (n - 1 ):
for j in range (i + 1 , n):
if (math.gcd(arr[i], arr[j]) = = 1 ):
return True
return False
if __name__ = = "__main__" :
n = 3
arr = [ 6 , 9 , 15 ]
if (hasCoprimePair(arr, n)):
print ( 1 )
else :
print (n)
|
C#
using System;
class GFG{
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static bool hasCoprimePair( int []arr, int n)
{
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((__gcd(arr[i],
arr[j])) == 1)
{
return true ;
}
}
}
return false ;
}
public static void Main(String[] args)
{
int n = 3;
int []arr = { 6, 9, 15 };
if (hasCoprimePair(arr, n))
{
Console.Write(1 + "\n" );
}
else
{
Console.Write(n + "\n" );
}
}
}
|
Javascript
<script>
function __gcd(a , b) {
return b == 0 ? a : __gcd(b, a % b);
}
function hasCoprimePair(arr , n) {
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if ((__gcd(arr[i], arr[j])) == 1) {
return true ;
}
}
}
return false ;
}
var n = 3;
var arr = [ 6, 9, 15 ];
if (hasCoprimePair(arr, n)) {
document.write(1 + "\n" );
}
else {
document.write(n + "\n" );
}
</script>
|
Time Complexity: O(N2 * log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...