📌  相关文章
📜  通过交换 GCD 等于最小数组元素的对,重新排列数组以使其不减少

📅  最后修改于: 2021-09-06 06:09:24             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是通过交换对(arr[i], arr[j])使数组不递减,使得i != j (1 ≤ i, j ≤ n)GCD (arr[i], arr[j])等于数组中存在的最小元素。

例子:

方法:

  1. 首先,遍历数组找到最小元素。将所有这些元素存储在另一个数组中并对该数组进行排序。
  2. 对于每个数组元素,检查它是否在正确的位置。如果发现是真的,则继续下一个元素。否则,检查它是否可以被数组的最小元素整除,因为只有这些元素将具有 GCD,其他元素等于数组的最小元素。
  3. 如果任何数组元素不在其正确位置并且该元素不能被数组的最小元素整除,则打印“否”。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to check if the array
// can be made non-decreasing
bool check(int a[], int n)
{
    int b[n];
    int minElement = INT_MAX;
 
    // Iterate till N
    for(int i = 0; i < n; i++)
    {
         
        // Find the minimum element
        b[i] = a[i];
        minElement = min(minElement, a[i]);
    }
 
    // Sort the array
    sort(b, b + n);
    int k = 1;
 
    // Iterate till N
    for(int i = 0; i < n; i++)
    {
         
        // Check if the element is
        // at its correct position
        if (a[i] != b[i] &&
            a[i] % minElement != 0)
        {
            k = 0;
            break;
        }
    }
     
    // Return the answer
    return k == 1 ? true : false;
}
 
// Driver Code
int main()
{
    int a[] = { 4, 3, 6, 6, 2, 9 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    // Print the answer
    if (check(a, n) == true)
        cout << "Yes \n";
    else
        cout<<"No \n";
 
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java program for above approach
 
import java.io.*;
import java.util.*;
import java.math.*;
 
class GFG {
 
    // Function to check if the array
    // can be made non-decreasing
    public static boolean check(int[] a, int n)
    {
        int[] b = new int[n];
        int minElement = Integer.MAX_VALUE;
 
        // Iterate till N
        for (int i = 0; i < n; i++) {
            // Find the minimum element
            b[i] = a[i];
            minElement
                = Math.min(minElement, a[i]);
        }
 
        // Sort the array
        Arrays.sort(b);
        int k = 1;
 
        // Iterate till N
        for (int i = 0; i < n; i++) {
            // Check if the element is
            // at its correct position
            if (a[i] != b[i]
                && a[i] % minElement != 0) {
                k = 0;
                break;
            }
        }
 
        // Return the answer
        return k == 1 ? true : false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] a = { 4, 3, 6, 6, 2, 9 };
 
        int n = a.length;
 
        // Print the answer
        if (check(a, n) == true) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}


Python3
# Python3 program for above approach
import sys
 
# Function to check if the array
# can be made non-decreasing
def check(a, n):
 
    b = [None] * n
    minElement = sys.maxsize
 
    # Iterate till N
    for i in range(0, n):
         
        # Find the minimum element
        b[i] = a[i]
        minElement = min(minElement, a[i])
 
    # Sort the array
    b.sort()
    k = 1
 
    # Iterate till N
    for i in range(0, n):
         
        # Check if the element is
        # at its correct position
        if ((a[i] != b[i]) and
            (a[i] % minElement != 0)):
            k = 0
            break
 
    # Return the answer
    if k == 1:
        return True
    else:
        return False
 
# Driver Code
if __name__ == "__main__":
 
    a = [ 4, 3, 6, 6, 2, 9 ]
 
    n = len(a)
 
    # Print the answer
    if check(a, n) == True:
        print("Yes")
    else:
        print("No")
 
# This code is contributed by akhilsaini


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to check if the array
// can be made non-decreasing
static bool check(int[] a, int n)
{
    int[] b = new int[n];
    int minElement = int.MaxValue;
 
    // Iterate till N
    for(int i = 0; i < n; i++)
    {
         
        // Find the minimum element
        b[i] = a[i];
        minElement = Math.Min(minElement, a[i]);
    }
 
    // Sort the array
    Array.Sort(b);
    int k = 1;
 
    // Iterate till N
    for(int i = 0; i < n; i++)
    {
         
        // Check if the element is
        // at its correct position
        if (a[i] != b[i] &&
            a[i] % minElement != 0)
        {
            k = 0;
            break;
        }
    }
 
    // Return the answer
    return k == 1 ? true : false;
}
 
// Driver Code
static public void Main()
{
    int[] a = { 4, 3, 6, 6, 2, 9 };
 
    int n = a.Length;
 
    // Print the answer
    if (check(a, n) == true)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
Yes

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live