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

📅  最后修改于: 2021-04-17 15:00:02             🧑  作者: Mango

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

例子:

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

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

下面是上述方法的实现:

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.


输出:
Yes

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