📌  相关文章
📜  检查是否所有数组元素都可以减少到少于X

📅  最后修改于: 2021-04-22 01:06:31             🧑  作者: Mango

给定一个由N个正整数和一个整数X组成的数组A [] ,任务是通过执行以下操作来确定是否有可能将所有数组元素转换为小于X的元素:

  • 选择2个不同的索引jk
  • 选择一个索引i ,其中A [i]> X。
  • 当且仅当gcd(A [j],A [k])≠1时,才替换A [i] = gcd(A [j],A [k])

例子:

方法:请按照以下步骤解决问题:

  1. 找到具有GCD≠1两个数以及GCD≤X,然后,通过使用两个数字,所需数量的A [I]可以用满足gcd代替(A [j]时,A [K])。
  2. 利用gcd(x,y)≤min(x,y)的事实,可以将数组元素减少到≤X
  3. 这样,可以使用步骤2将数组的其余部分转换为≤X

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if all array
// elements are≤ X
bool check(int A[], int X, int N)
{
    for(int i = 0; i < N; i++)
    {
        if (A[i] > X)
        {
            return false;
        }
    }
    return true;
}
 
// Function to check if all array elements
// can be reduced to less than X or not
bool findAns(int A[], int N, int X)
{
     
    // Checks if all array elements
    // are already ≤ X or not
    if (check(A, X, N))
    {
        return true;
    }
 
    // Traverse every possible pair
    for(int i = 0; i < N; i++)
    {
        for(int j = i + 1; j < N; j++)
        {
             
            // Calculate GCD of two
            // array elements
            int g = __gcd(A[i], A[j]);
 
            // If gcd is ≠ 1
            if (g != 1)
            {
                 
                // If gcd is ≤ X, then a pair
                // is present to reduce all
                // array elements to ≤ X
                if (g <= X)
                {
                    return true;
                }
            }
        }
    }
     
    // If no pair is present
    // with gcd is ≤ X
    return false;
}
 
// Driver Code
int main()
{
    int X = 4;
    int A[] = { 2, 1, 5, 3, 6 };
    int N = 5;
     
    if (findAns(A, N, X))
    {
        cout << "true";
    }
    else
    {
        cout << "false";
    }
}
 
// This code is contributed by mohit kumar 29


Java
// Java Program to implement
// the above approach
 
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
    // Function to check if all array elements
    // can be reduced to less than X or not
    public static boolean findAns(
        int[] A, int N, int X)
    {
        // Checks if all array elements
        // are already ≤ X or not
        if (check(A, X)) {
            return true;
        }
 
        // Traverse every possible pair
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
 
                // Calculate GCD of two
                // array elements
                int gcd = gcd(A[i], A[j]);
 
                // If gcd is ≠ 1
                if (gcd != 1) {
 
                    // If gcd is ≤ X, then a pair
                    // is present to reduce all
                    // array elements to ≤ X
                    if (gcd <= X) {
 
                        return true;
                    }
                }
            }
        }
 
        // If no pair is present
        // with gcd is ≤ X
        return false;
    }
 
    // Function to check if all array elements are≤ X
    public static boolean check(int[] A, int X)
    {
        for (int i = 0; i < A.length; i++) {
            if (A[i] > X) {
                return false;
            }
        }
        return true;
    }
 
    // Function to calculate gcd of two numbers
    public static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int X = 4;
        int[] A = { 2, 1, 5, 3, 6 };
        int N = 5;
 
        System.out.println(findAns(A, N, X));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to check if all array elements
# can be reduced to less than X or not
def findAns(A, N, X):
   
  # Checks if all array elements
  # are already ≤ X or not
  if (check(A, X)):
    return True
         
  # Traverse every possible pair
  for i in range(N):
    for j in range(i + 1, N):
       
      # Calculate GCD of two
      # array elements
      gcd = GCD(A[i], A[j])
 
      # If gcd is ≠ 1
      if (gcd != 1):
         
        # If gcd is ≤ X, then a pair
        # is present to reduce all
        # array elements to ≤ X
        if (gcd <= X):
          return True
 
  # If no pair is present
  # with gcd is ≤ X
  return False
 
# Function to check if all array elements are≤ X
def check(A, X):
  for i in range(len(A)):
    if (A[i] > X):
      return False
  return True
 
# Function to calculate gcd of two numbers
def GCD(a, b):
  if (b == 0):
    return a
  return GCD(b, a % b)
 
# Driver Code
X = 4
A = [ 2, 1, 5, 3, 6 ]
N = 5
 
print(findAns(A, N, X))
 
# This code is contributed by rohitsingh07052


C#
// C# Program to implement
// the above approach
using System;
class GFG {
 
    // Function to check if all array elements
    // can be reduced to less than X or not
    public static bool findAns(
        int[] A, int N, int X)
    {
        // Checks if all array elements
        // are already ≤ X or not
        if (check(A, X))
        {
            return true;
        }
 
        // Traverse every possible pair
        for (int i = 0; i < N; i++)
        {
            for (int j = i + 1; j < N; j++)
            {
 
                // Calculate GCD of two
                // array elements
                int gcd = gcdFoo(A[i], A[j]);
 
                // If gcd is ≠ 1
                if (gcd != 1)
                {
 
                    // If gcd is ≤ X, then a pair
                    // is present to reduce all
                    // array elements to ≤ X
                    if (gcd <= X)
                    {
                        return true;
                    }
                }
            }
        }
 
        // If no pair is present
        // with gcd is ≤ X
        return false;
    }
 
    // Function to check if all array elements are≤ X
    public static bool check(int[] A, int X)
    {
        for (int i = 0; i < A.Length; i++)
        {
            if (A[i] > X)
            {
                return false;
            }
        }
        return true;
    }
 
    // Function to calculate gcd of two numbers
    static int gcdFoo(int a, int b)
    {
        if (b == 0)
            return a;
        return gcdFoo(b, a % b);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int X = 4;
        int[] A = { 2, 1, 5, 3, 6 };
        int N = 5;
 
        Console.WriteLine(findAns(A, N, X));
    }
}
 
// This code is contributed by 29AjayKumar


输出
true

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