📌  相关文章
📜  检查是否可以通过交换GCD等于数组中最小元素的对来对数组进行排序

📅  最后修改于: 2021-05-17 19:54:48             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是检查是否可以通过仅交换GCD(最大公约数)等于数组最小元素的元素来对数组进行排序。如果可以对数组排序,则打印“是”。否则,打印“否”。

例子:

方法:想法是从数组中找到最小的元素,并检查是否可以对数组进行排序。步骤如下:

  1. 找到数组的最小元素并将其存储在变量中,例如mn
  2. 将数组存储在一个临时数组中,例如B []
  3. 排序原始数组arr []
  4. 现在,遍历数组,如果有一个元素不能被mn整除并且其位置在排序后发生了变化,则打印“ NO”。否则,通过与其他元素交换,重新排列可被mn整除的元素。
  5. 如果所有不能被mn整除的元素的位置保持不变,则打印“是”。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to check if it is
// possible to sort array or not
void isPossible(int arr[], int N)
{
 
    // Store the smallest element
    int mn = INT_MAX;
 
    // Copy the original array
    int B[N];
 
    // Iterate over the given array
    for (int i = 0; i < N; i++) {
 
        // Update smallest element
        mn = min(mn, arr[i]);
 
        // Copy elements of arr[]
        // to array B[]
        B[i] = arr[i];
    }
 
    // Sort original array
    sort(arr, arr + N);
 
    // Iterate over the given array
    for (int i = 0; i < N; i++) {
 
        // If the i-th element is not
        // in its sorted place
        if (arr[i] != B[i]) {
 
            // Not possible to swap
            if (B[i] % mn != 0) {
                cout << "No";
                return;
            }
        }
    }
 
    cout << "Yes";
    return;
}
 
// Driver Code
int main()
{
    // Given array
    int N = 6;
    int arr[] = { 4, 3, 6, 6, 2, 9 };
 
    // Function Call
    isPossible(arr, N);
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function to check if it is
// possible to sort array or not
static void isPossible(int arr[],
                       int N)
{
  // Store the smallest element
  int mn = Integer.MAX_VALUE;
 
  // Copy the original array
  int []B = new int[N];
 
  // Iterate over the given array
  for (int i = 0; i < N; i++)
  {
    // Update smallest element
    mn = Math.min(mn, arr[i]);
 
    // Copy elements of arr[]
    // to array B[]
    B[i] = arr[i];
  }
 
  // Sort original array
  Arrays.sort(arr);
 
  // Iterate over the given array
  for (int i = 0; i < N; i++)
  {
    // If the i-th element is not
    // in its sorted place
    if (arr[i] != B[i])
    {
      // Not possible to swap
      if (B[i] % mn != 0)
      {
        System.out.print("No");
        return;
      }
    }
  }
  System.out.print("Yes");
  return;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array
  int N = 6;
  int arr[] = {4, 3, 6, 6, 2, 9};
 
  // Function Call
  isPossible(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of
# the above approach
import sys
 
# Function to check if it is
# possible to sort array or not
def isPossible(arr, N):
   
    # Store the smallest element
    mn = sys.maxsize;
 
    # Copy the original array
    B = [0] * N;
 
    # Iterate over the given array
    for i in range(N):
       
        # Update smallest element
        mn = min(mn, arr[i]);
 
        # Copy elements of arr
        # to array B
        B[i] = arr[i];
 
    # Sort original array
    arr.sort();
 
    # Iterate over the given array
    for i in range(N):
       
        # If the i-th element is not
        # in its sorted place
        if (arr[i] != B[i]):
           
            # Not possible to swap
            if (B[i] % mn != 0):
                print("No");
                return;
 
    print("Yes");
    return;
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    N = 6;
    arr = [4, 3, 6, 6, 2, 9];
 
    # Function Call
    isPossible(arr, N);
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to check if it is
// possible to sort array or not
static void isPossible(int []arr,
                       int N)
{
  // Store the smallest element
  int mn = int.MaxValue;
 
  // Copy the original array
  int []B = new int[N];
 
  // Iterate over the given array
  for (int i = 0; i < N; i++)
  {
    // Update smallest element
    mn = Math.Min(mn, arr[i]);
 
    // Copy elements of []arr
    // to array []B
    B[i] = arr[i];
  }
 
  // Sort original array
  Array.Sort(arr);
 
  // Iterate over the given array
  for (int i = 0; i < N; i++)
  {
    // If the i-th element is not
    // in its sorted place
    if (arr[i] != B[i])
    {
      // Not possible to swap
      if (B[i] % mn != 0)
      {
        Console.Write("No");
        return;
      }
    }
  }
  Console.Write("Yes");
  return;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array
  int N = 6;
  int []arr = {4, 3, 6, 6, 2, 9};
 
  // Function Call
  isPossible(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes

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