📌  相关文章
📜  最小化要添加到 Array 中的整数计数,以使每个相邻对互质

📅  最后修改于: 2022-05-13 01:56:08.418000             🧑  作者: Mango

最小化要添加到 Array 中的整数计数,以使每个相邻对互质

给定一个包含N个整数的整数数组arr[] ,任务是
通过在数组中添加最小数量的整数,使数组中的每个相邻对互质。如果不可能,则返回 -1。

例子:

方法:给定的问题可以通过一些观察和基本的数论来解决。可以按照以下步骤进行:

  • 以非降序对数组进行排序
  • 从左到右迭代并检查每个相邻对的以下条件:
  • 如果当前对是互质的,则不需要添加任何额外的元素
  • 如果当前对不是互质的,那么 迭代元素之间的所有值并检查当前值是否与左右对互质。
  • 否则,总是可以添加两个互质且分别与左值和右值互质的值。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
 
#include 
using namespace std;
 
// Function to calculate minimum additions
// required such that no adjacent pairs are
// coprime in their sorted order
int MinimumAdditions(int arr[], int n)
{
    // Sort the given array
    // in non-decreasing order
    sort(arr, arr + n);
 
    // First check if any two adjacent
    // elements are same then
    // the answer will be -1
    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1]) {
 
            // Return directly from here
            return -1;
        }
    }
 
    // Variable to store the answer
    int ans = 0;
 
    for (int i = 1; i < n; i++) {
        if (__gcd(arr[i],
                  arr[i - 1])
            == 1) {
            continue;
        }
 
        // Check for a single middle element
        // Maintain a bool value
        bool found = 0;
 
        for (int j = arr[i - 1] + 1;
             j <= arr[i] - 1; j++) {
            if (__gcd(arr[i - 1], j) == 1
                && __gcd(j, arr[i]) == 1) {
                found = 1;
            }
        }
        if (found) {
            ans++;
        }
        else {
            ans += 2;
        }
    }
 
    // return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { 2200, 42, 2184, 17 };
    cout << MinimumAdditions(arr, N);
}


Java
// Java implementation for the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG{
 
static int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
 
// Function to calculate minimum additions
// required such that no adjacent pairs are
// coprime in their sorted order
static int MinimumAdditions(int []arr, int n)
{
    // Sort the given array
    // in non-decreasing order
    Arrays.sort(arr);
 
    // First check if any two adjacent
    // elements are same then
    // the answer will be -1
    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1]) {
 
            // Return directly from here
            return -1;
        }
    }
 
    // Variable to store the answer
    int ans = 0;
 
    for (int i = 1; i < n; i++) {
        if (gcd(arr[i],
                  arr[i - 1])
            == 1) {
            continue;
        }
 
        // Check for a single middle element
        // Maintain a bool value
        int found = 0;
 
        for (int j = arr[i - 1] + 1;
             j <= arr[i] - 1; j++) {
            if (gcd(arr[i - 1], j) == 1
                && gcd(j, arr[i]) == 1) {
                found = 1;
            }
        }
        if (found!=0) {
            ans++;
        }
        else {
            ans += 2;
        }
    }
 
    // return the answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
    {
    int N = 4;
    int []arr = { 2200, 42, 2184, 17 };
    System.out.println(MinimumAdditions(arr, N));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python 3 implementation for the above approach
import math
 
# Function to calculate minimum additions
# required such that no adjacent pairs are
# coprime in their sorted order
def MinimumAdditions(arr, n):
 
    # Sort the given array
    # in non-decreasing order
    arr.sort()
 
    # First check if any two adjacent
    # elements are same then
    # the answer will be -1
    for i in range(1,  n):
        if (arr[i] == arr[i - 1]):
 
            # Return directly from here
            return -1
 
    # Variable to store the answer
    ans = 0
 
    for i in range(1, n):
        if (math.gcd(arr[i],
                     arr[i - 1])
                == 1):
            continue
 
        # Check for a single middle element
        # Maintain a bool value
        found = 0
 
        for j in range(arr[i - 1] + 1,
                       arr[i]):
            if (math.gcd(arr[i - 1], j) == 1
                    and math.gcd(j, arr[i]) == 1):
                found = 1
 
        if (found):
            ans += 1
 
        else:
            ans += 2
 
    # return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    arr = [2200, 42, 2184, 17]
    print(MinimumAdditions(arr, N))
 
    # This code is contributed by ukasp.


C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
 
// Function to calculate minimum additions
// required such that no adjacent pairs are
// coprime in their sorted order
static int MinimumAdditions(int []arr, int n)
{
    // Sort the given array
    // in non-decreasing order
    Array.Sort(arr);
 
    // First check if any two adjacent
    // elements are same then
    // the answer will be -1
    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1]) {
 
            // Return directly from here
            return -1;
        }
    }
 
    // Variable to store the answer
    int ans = 0;
 
    for (int i = 1; i < n; i++) {
        if (gcd(arr[i],
                  arr[i - 1])
            == 1) {
            continue;
        }
 
        // Check for a single middle element
        // Maintain a bool value
        int found = 0;
 
        for (int j = arr[i - 1] + 1;
             j <= arr[i] - 1; j++) {
            if (gcd(arr[i - 1], j) == 1
                && gcd(j, arr[i]) == 1) {
                found = 1;
            }
        }
        if (found!=0) {
            ans++;
        }
        else {
            ans += 2;
        }
    }
 
    // return the answer
    return ans;
}
 
// Driver Code
public static void Main()
{
    int N = 4;
    int []arr = { 2200, 42, 2184, 17 };
    Console.Write(MinimumAdditions(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出
3

时间复杂度: O(Nlog(N) + M),其中 M 是数组中的最大元素
辅助空间: O(1)