📌  相关文章
📜  检查是否可以通过交换其中一个数组的对来使两个数组相等

📅  最后修改于: 2021-09-06 05:15:42             🧑  作者: Mango

给定两个二进制阵列ARR1 []ARR2 []相同的尺寸,该任务是使两个阵列通过交换对ARR1的等于[]仅当ARR1 [I] = 0ARR1 [J] = 1(0≤ i < j < N) )。如果可以使两个数组相等,则打印“Yes” 。否则,打印“否”

例子:

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

  • 初始化两个变量,比如countflag (= true )。
  • 遍历数组并对每个数组元素执行以下操作:
    • 如果 arr1[i] != arr2[i]:
      • 如果arr1[i] == 0 ,则将count增加1
      • 否则,将count1 ,如果count < 0 ,则更新flag = false
  • 如果flag等于true ,则打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to check if two arrays
// can be made equal or not by swapping
// pairs of only one of the arrays
void checkArrays(int arr1[], int arr2[], int N)
{
    // Stores elements required
    // to be replaced
    int count = 0;
 
    // To check if the arrays
    // can be made equal or not
    bool flag = true;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If array elements are not equal
        if (arr1[i] != arr2[i]) {
 
            if (arr1[i] == 0)
 
                // Increment count by 1
                count++;
            else {
 
                // Decrement count by 1
                count--;
                if (count < 0) {
                    flag = 0;
                    break;
                }
            }
        }
    }
 
    // If flag is true and count is 0,
    // print "Yes". Otherwise "No"
    if (flag && count == 0)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    // Given arrays
    int arr1[] = { 0, 0, 1, 1 };
    int arr2[] = { 1, 1, 0, 0 };
 
    // Size of the array
    int N = sizeof(arr1) / sizeof(arr1[0]);
    checkArrays(arr1, arr2, N);
 
    return 0;
}


Java
// Java program for above approach
public class GFG
{
 
  // Function to check if two arrays
  // can be made equal or not by swapping
  // pairs of only one of the arrays
  static void checkArrays(int arr1[], int arr2[], int N)
  {
 
    // Stores elements required
    // to be replaced
    int count = 0;
 
    // To check if the arrays
    // can be made equal or not
    boolean flag = true;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // If array elements are not equal
      if (arr1[i] != arr2[i])
      {
        if (arr1[i] == 0)
 
          // Increment count by 1
          count++;
        else
        {
 
          // Decrement count by 1
          count--;
          if (count < 0)
          {
            flag = false;
            break;
          }
        }
      }
    }
 
    // If flag is true and count is 0,
    // print "Yes". Otherwise "No"
    if ((flag && (count == 0)) == true)
      System.out.println("Yes");
    else
      System.out.println("No");
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given arrays
    int arr1[] = { 0, 0, 1, 1 };
    int arr2[] = { 1, 1, 0, 0 };
 
    // Size of the array
    int N = arr1.length;  
    checkArrays(arr1, arr2, N);
  }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for above approach
 
# Function to check if two arrays
# can be made equal or not by swapping
# pairs of only one of the arrays
def checkArrays(arr1, arr2, N):
   
    # Stores elements required
    # to be replaced
    count = 0
 
    # To check if the arrays
    # can be made equal or not
    flag = True
 
    # Traverse the array
    for i in range(N):
 
        # If array elements are not equal
        if (arr1[i] != arr2[i]):
 
            if (arr1[i] == 0):
 
                # Increment count by 1
                count += 1
            else:
 
                # Decrement count by 1
                count -= 1
                if (count < 0):
                    flag = 0
                    break
 
    # If flag is true and count is 0,
    # pr"Yes". Otherwise "No"
    if (flag and count == 0):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    # Given arrays
    arr1 = [0, 0, 1, 1]
    arr2 = [1, 1, 0, 0]
 
    # Size of the array
    N = len(arr1)
 
    checkArrays(arr1, arr2, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if two arrays
  // can be made equal or not by swapping
  // pairs of only one of the arrays
  static void checkArrays(int[] arr1, int[] arr2, int N)
  {
 
    // Stores elements required
    // to be replaced
    int count = 0;
 
    // To check if the arrays
    // can be made equal or not
    bool flag = true;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // If array elements are not equal
      if (arr1[i] != arr2[i])
      {
        if (arr1[i] == 0)
 
          // Increment count by 1
          count++;
        else
        {
 
          // Decrement count by 1
          count--;
          if (count < 0)
          {
            flag = false;
            break;
          }
        }
      }
    }
 
    // If flag is true and count is 0,
    // print "Yes". Otherwise "No"
    if ((flag && (count == 0)) == true)
      Console.WriteLine("Yes");
    else
      Console.WriteLine("No");
  }
 
// Driver Code
static public void Main()
{
    // Given arrays
    int[] arr1 = { 0, 0, 1, 1 };
    int[] arr2 = { 1, 1, 0, 0 };
 
    // Size of the array
    int N = arr1.Length;  
    checkArrays(arr1, arr2, N);
}
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
Yes

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

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