📌  相关文章
📜  通过交换包含另一个数组中不相等元素的索引中的对来检查数组是否可以排序

📅  最后修改于: 2021-04-17 18:43:56             🧑  作者: Mango

给定大小N和大小为N的二进制数组B [][]数组A中,任务是检查,如果数组A []可以通过交换对被转换成一个排序后的数组(A [I],A [J ])如果B [i]不等于B [j] 。如果可以对数组A []进行排序,则打印“”。否则,打印“ No ”。

例子:

方法:可以根据以下观察结果解决问题:

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

  • 检查给定数组A []是否已按升序排序。如果发现是真的,则打印“是”
  • 否则,计算数组B []中存在的1 s和0 s的数量。
    • 如果数组B []至少包含一个0和一个1 ,则打印“是”
    • 否则,打印“否”

下面是上述方法的实现:

C++
// C++ Program for above approach
#include 
using namespace std;
 
// Function to check if array, A[] can be converted
// into sorted array by swapping (A[i], A[j]) if B[i]
// not equal to B[j]
bool checkifSorted(int A[], int B[], int N)
{
 
  // Stores if array A[] is sorted
  // in descending order or not
  bool flag = false;
 
  // Traverse the array A[]
  for (int i = 0; i < N - 1; i++) {
 
    // If A[i] is greater than A[i + 1]
    if (A[i] > A[i + 1]) {
 
      // Update flag
      flag = true;
      break;
    }
  }
 
  // If array is sorted
  // in ascending order
  if (!flag) {
    return true;
  }
 
  // count = 2: Check if 0s and 1s
  // both present in the B[]
  int count = 0;
 
  // Traverse the array
  for (int i = 0; i < N; i++) {
 
    // If current element is 0
    if (B[i] == 0) {
 
      // Update count
      count++;
      break;
    }
  }
 
 
  // Traverse the array B[]
  for (int i = 0; i < N; i++) {
 
    // If current element is 1
    if (B[i] == 1) {
      count++;
      break;
    }
  }
 
  // If both 0s and 1s are present
  // in the array
  if (count == 2) {
    return true;
  }
  return false;
}
 
// Driver Code
int main()
{
  // Input array A[]
  int A[] = { 3, 1, 2 };
 
  // Input array B[]
  int B[] = { 0, 1, 1 };
 
  int N = sizeof(A) / sizeof(A[0]);
  // Function call
  bool check = checkifSorted(A, B, N);
 
  // If true,print YES
  if (check) {
    cout << "YES" <


Java
// Java program of the above approach
import java.io.*;
 
class GFG {
 
     
    // Function to check if array, A[] can be converted
    // into sorted array by swapping (A[i], A[j]) if B[i]
    // not equal to B[j]
    static boolean checkifSorted(int A[], int B[], int N)
    {
         
        // Stores if array A[] is sorted
        // in descending order or not
        boolean flag = false;
 
        // Traverse the array A[]
        for (int i = 0; i < N - 1; i++) {
 
            // If A[i] is greater than A[i + 1]
            if (A[i] > A[i + 1]) {
 
                // Update flag
                flag = true;
                break;
            }
        }
 
        // If array is sorted
        // in ascending order
        if (!flag) {
            return true;
        }
 
        // count = 2: Check if 0s and 1s
        // both present in the B[]
        int count = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
             
            // If current element is 0
            if (B[i] == 0) {
                 
                // Update count
                count++;
                break;
            }
        }
         
         
        // Traverse the array B[]
        for (int i = 0; i < N; i++) {
             
            // If current element is 1
            if (B[i] == 1) {
                count++;
                break;
            }
        }
 
        // If both 0s and 1s are present
        // in the array
        if (count == 2) {
            return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input array A[]
        int A[] = { 3, 1, 2 };
 
        // Input array B[]
        int B[] = { 0, 1, 1 };
 
        int N = A.length;
        // Function call
        boolean check = checkifSorted(A, B, N);
 
        // If true,print YES
        if (check) {
            System.out.println("YES");
        }
        // Else print NO
        else {
            System.out.println("NO");
        }
    }
}


Python3
# Python program of the above approach
     
# Function to check if array, A[] can be converted
# into sorted array by swapping (A[i], A[j]) if B[i]
# not equal to B[j]
def checkifSorted(A, B, N):
   
  # Stores if array A[] is sorted
  # in descending order or not
  flag = False
 
  # Traverse the array A[]
  for i in range( N - 1):
     
    # If A[i] is greater than A[i + 1]
    if (A[i] > A[i + 1]):
       
      # Update flag
      flag = True
      break
 
  # If array is sorted
  # in ascending order
  if (not flag):
    return True
 
  # count = 2: Check if 0s and 1s
  # both present in the B[]
  count = 0
 
  # Traverse the array
  for i in range(N):
     
    # If current element is 0
    if (B[i] == 0):
       
      # Update count
      count += 1
      break
         
  # Traverse the array B[]
  for i in range(N):
     
    # If current element is 1
    if B[i]:
      count += 1
      break
       
  # If both 0s and 1s are present
  # in the array
  if (count == 2):
    return True
       
  return False
 
# Driver Code
# Input array A[]
A = [ 3, 1, 2 ]
 
# Input array B[]
B = [ 0, 1, 1 ]
N = len(A)
 
# Function call
check = checkifSorted(A, B, N)
 
# If true,print YES
if (check):
  print("YES")
         
# Else print NO
else:
  print("NO")
  
# This code is contributed by rohitsingh07052


C#
// C# program of the above approach
using System;
public class GFG
{
     
    // Function to check if array, A[] can be converted
    // into sorted array by swapping (A[i], A[j]) if B[i]
    // not equal to B[j]
    static bool checkifSorted(int []A, int []B, int N)
    {
         
        // Stores if array A[] is sorted
        // in descending order or not
        bool flag = false;
 
        // Traverse the array A[]
        for (int i = 0; i < N - 1; i++) {
 
            // If A[i] is greater than A[i + 1]
            if (A[i] > A[i + 1]) {
 
                // Update flag
                flag = true;
                break;
            }
        }
 
        // If array is sorted
        // in ascending order
        if (!flag) {
            return true;
        }
 
        // count = 2: Check if 0s and 1s
        // both present in the B[]
        int count = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
             
            // If current element is 0
            if (B[i] == 0) {
                 
                // Update count
                count++;
                break;
            }
        }
         
         
        // Traverse the array B[]
        for (int i = 0; i < N; i++)
        {
             
            // If current element is 1
            if (B[i] == 1)
            {
                count++;
                break;
            }
        }
 
        // If both 0s and 1s are present
        // in the array
        if (count == 2)
        {
            return true;
        }
        return false;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        // Input array A[]
        int []A = { 3, 1, 2 };
 
        // Input array B[]
        int []B = { 0, 1, 1 };
        int N = A.Length;
         
        // Function call
        bool check = checkifSorted(A, B, N);
 
        // If true,print YES
        if (check) {
            Console.WriteLine("YES");
        }
        // Else print NO
        else {
            Console.WriteLine("NO");
        }
    }
}
 
// This code is contributed by AnkThon


输出:
YES

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