📌  相关文章
📜  通过用 1 重复替换互质对来最小化数组长度

📅  最后修改于: 2021-10-26 05:19:05             🧑  作者: Mango

给定一个由N 个元素组成的数组arr[] ,任务是通过用1替换任意两个互质数组元素来最小化数组长度。
例子:

朴素方法:最简单的方法是遍历数组并检查互质对。如果找到,则用 1 次搜索替换下一个互质对,依此类推。

时间复杂度: O(N 3 * log 否)
辅助空间: O(1)

高效的方法:这种方法基于以下事实:

这个想法是找出数组中是否存在任何互质对。如果找到,则可以基于上述事实将所有数组元素减少到1。因此,如果找到任何互质对,则所需的答案将是 1,否则,答案将是数组的初始大小。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to find the final array
// length by replacing coprime pair with 1
bool hasCoprimePair(vector& arr, int n)
{
 
    // Iterate over all pairs of element
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Check if gcd is 1
            if (__gcd(arr[i], arr[j]) == 1) {
                return true;
            }
        }
    }
 
    // If no coprime pair
    // found return false
    return false;
}
 
// Driver code
int main()
{
 
    int n = 3;
    vector arr = { 6, 9, 15 };
 
    // Check if atleast one coprime
    // pair exists in the array
    if (hasCoprimePair(arr, n)) {
        cout << 1 << endl;
    }
 
    // If no such pair exists
    else {
        cout << n << endl;
    }
}


Java
// Java Program for the above approach
import java.util.*;
class GFG{
     
// Recursive function to return
// gcd of a and b 
static int __gcd(int a, int b) 
{ 
    return b == 0? a:__gcd(b, a % b);    
}
 
// Function to find the final array
// length by replacing coprime pair with 1
static boolean hasCoprimePair(int []arr, int n)
{
 
    // Iterate over all pairs of element
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
 
            // Check if gcd is 1
            if ((__gcd(arr[i], arr[j])) == 1)
            {
                return true;
            }
        }
    }
 
    // If no coprime pair
    // found return false
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    int []arr = { 6, 9, 15 };
 
    // Check if atleast one coprime
    // pair exists in the array
    if (hasCoprimePair(arr, n))
    {
        System.out.print(1 + "\n");
    }
 
    // If no such pair exists
    else
    {
        System.out.print(n + "\n");
    }
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
import math
 
# Function to find the final array
# length by replacing coprime pair with 1
def hasCoprimePair(arr, n):
 
    # Iterate over all pairs of element
    for i in range(n - 1):
        for j in range(i + 1, n):
 
            # Check if gcd is 1
            if (math.gcd(arr[i], arr[j]) == 1):
                return True
             
    # If no coprime pair
    # found return false
    return False
 
# Driver code
if __name__ == "__main__":
 
    n = 3
    arr = [ 6, 9, 15 ]
 
    # Check if atleast one coprime
    # pair exists in the array
    if (hasCoprimePair(arr, n)):
        print(1)
     
    # If no such pair exists
    else:
        print(n)
     
# This code is contributed by chitranayal


C#
// C# Program for the above approach
using System;
class GFG{
     
// Recursive function to return
// gcd of a and b 
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Function to find the readonly array
// length by replacing coprime pair with 1
static bool hasCoprimePair(int []arr, int n)
{
 
    // Iterate over all pairs of element
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
 
            // Check if gcd is 1
            if ((__gcd(arr[i],
                       arr[j])) == 1)
            {
                return true;
            }
        }
    }
 
    // If no coprime pair
    // found return false
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    int []arr = { 6, 9, 15 };
 
    // Check if atleast one coprime
    // pair exists in the array
    if (hasCoprimePair(arr, n))
    {
        Console.Write(1 + "\n");
    }
 
    // If no such pair exists
    else
    {
        Console.Write(n + "\n");
    }
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
3

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

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