📌  相关文章
📜  检查是否可以通过交换不等元素对将数组转换为另一个给定数组

📅  最后修改于: 2021-09-03 15:00:11             🧑  作者: Mango

给定两个大小为N 的数组arr1[]arr2[] ,由二进制整数组成,任务是检查arr1[] 是否可以通过交换任何数组元素对(arr1[i], arr1[ ]转换为arr2[] j])使得i < jarr1[i]1arr1[j]0 (任意次数)。如果可以这样做,则打印“是” 。否则,打印“否”

例子:

方法:解决此问题的想法基于以下观察:

  • 该操作不会改变数组arr1[]中 1 和 0 个数的频率,因此如果数组之间01的个数不同,它们永远不会与上述操作相等。
  • 如果arr2[] 的某个前缀包含比相同长度的arr1[]的前缀多的1 ,则不可能使arr1[]arr2[]相等,因为1只能向右移动。
  • 否则,在所有其他情况下,数组可以相等。

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

  • 初始化一个变量,比如用0计数,以存储arr1[]arr2[]的前缀和的差异。
  • 计算1s的数量 和在两个阵列和校验0如果在ARR1 1s0s []的数目不等于10的数量ARR2 [],然后打印“否”。
  • 使用变量i迭代范围[1, N – 1]并执行以下操作:
    • 将值(arr1[i] – arr2[i]) 添加到变量count
    • 如果 count 的值小于0 ,则打印“No”,否则继续下一个元素对。
  • 完成上述步骤后,如果任何步骤的计数都不是负数,则打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if arr1[] can be
// converted to arr2[] by swapping pair
// (i, j) such that i < j and arr[i] is
// 1 and arr[j] is 0
void canMakeEqual(int arr1[], int arr2[], int N)
{
    // Stores the differences of prefix
    // sum of arr1 and arr2
    int count = 0;
 
    // Stores the count of 1 and
    // zero of arr1
    int arr1_one = 0, arr1_zero = 0;
 
    // Stores the count of 1 and
    // zero of arr2
    int arr2_one = 0, arr2_zero = 0;
 
    // Iterate in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
 
        // If arr1[i] is 1, then
        // increment arr1_one by one
        if (arr1[i] == 1) {
            arr1_one++;
        }
 
        // Otherwise increment
        // arr1_zero by one
        else if (arr1[i] == 0) {
            arr1_zero++;
        }
 
        // If arr2[i] is 1, then
        // increment arr2_one by one
        if (arr2[i] == 1) {
            arr2_one++;
        }
 
        // Otherwise increment
        // arr2_zero by one
        else if (arr2[i] == 0) {
            arr2_zero++;
        }
    }
 
    // Check if number of 1s and 0s
    // of arr1 is equal to number of
    // 1s and 0s of arr2 respectievly
    if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
        cout << "No";
        return;
    }
 
    // Iterate over the range [0, N-1]
    for (int i = 0; i < N; i++) {
 
        // Increment count by differences
        // arr1[i] and arr2[i]
        count = count + (arr1[i] - arr2[i]);
 
        // Check if number of 1's in
        // arr2 are more than arr1 and
        // then print "No"
        if (count < 0) {
            cout << "No";
            return;
        }
    }
 
    // Finally, print "Yes"
    cout << "Yes";
}
 
// Driver Code
int main()
{
    // Given input arrays
    int arr1[] = { 0, 1, 1, 0 };
    int arr2[] = { 0, 0, 1, 1 };
 
    // Size of the array
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function Call
    canMakeEqual(arr1, arr2, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to check if arr1[] can be
  // converted to arr2[] by swapping pair
  // (i, j) such that i < j and arr[i] is
  // 1 and arr[j] is 0
  static void canMakeEqual(int []arr1, int []arr2, int N)
  {
 
    // Stores the differences of prefix
    // sum of arr1 and arr2
    int count = 0;
 
    // Stores the count of 1 and
    // zero of arr1
    int arr1_one = 0, arr1_zero = 0;
 
    // Stores the count of 1 and
    // zero of arr2
    int arr2_one = 0, arr2_zero = 0;
 
    // Iterate in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
 
      // If arr1[i] is 1, then
      // increment arr1_one by one
      if (arr1[i] == 1) {
        arr1_one++;
      }
 
      // Otherwise increment
      // arr1_zero by one
      else if (arr1[i] == 0) {
        arr1_zero++;
      }
 
      // If arr2[i] is 1, then
      // increment arr2_one by one
      if (arr2[i] == 1) {
        arr2_one++;
      }
 
      // Otherwise increment
      // arr2_zero by one
      else if (arr2[i] == 0) {
        arr2_zero++;
      }
    }
 
    // Check if number of 1s and 0s
    // of arr1 is equal to number of
    // 1s and 0s of arr2 respectievly
    if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
      System.out.print("No");
      return;
    }
 
    // Iterate over the range [0, N-1]
    for (int i = 0; i < N; i++) {
 
      // Increment count by differences
      // arr1[i] and arr2[i]
      count = count + (arr1[i] - arr2[i]);
 
      // Check if number of 1's in
      // arr2 are more than arr1 and
      // then print "No"
      if (count < 0) {
        System.out.print("No");
        return;
      }
    }
 
    // Finally, print "Yes"
    System.out.print("Yes");
  }
 
// Driver Code
public static void main(String[] args)
{
   
    // Given input arrays
    int []arr1 = { 0, 1, 1, 0 };
    int []arr2 = { 0, 0, 1, 1 };
 
    // Size of the array
    int N = arr1.length;
 
    // Function Call
    canMakeEqual(arr1, arr2, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python 3 program for the above approach
 
# Function to check if arr1[] can be
# converted to arr2[] by swapping pair
# (i, j) such that i < j and arr[i] is
# 1 and arr[j] is 0
def canMakeEqual(arr1, arr2, N):
   
    # Stores the differences of prefix
    # sum of arr1 and arr2
    count = 0
 
    # Stores the count of 1 and
    # zero of arr1
    arr1_one = 0
    arr1_zero = 0
 
    # Stores the count of 1 and
    # zero of arr2
    arr2_one = 0
    arr2_zero = 0
 
    # Iterate in the range [0, N - 1]
    for i in range(N):
       
        # If arr1[i] is 1, then
        # increment arr1_one by one
        if (arr1[i] == 1):
            arr1_one += 1
 
        # Otherwise increment
        # arr1_zero by one
        elif(arr1[i] == 0):
            arr1_zero += 1
 
        # If arr2[i] is 1, then
        # increment arr2_one by one
        if (arr2[i] == 1):
            arr2_one += 1
 
        # Otherwise increment
        # arr2_zero by one
        elif (arr2[i] == 0):
            arr2_zero += 1
 
    # Check if number of 1s and 0s
    # of arr1 is equal to number of
    # 1s and 0s of arr2 respectievly
    if (arr1_one != arr2_one or arr1_zero != arr2_zero):
        print("No")
        return
 
    # Iterate over the range [0, N-1]
    for i in range(N):
 
        # Increment count by differences
        # arr1[i] and arr2[i]
        count = count + (arr1[i] - arr2[i])
 
        # Check if number of 1's in
        # arr2 are more than arr1 and
        # then print "No"
        if (count < 0):
            print("No")
            return
 
    # Finally, print "Yes"
    print("Yes")
 
# Driver Code
if __name__ == '__main__':
   
    # Given input a
    arr1 =  [0, 1, 1, 0]
    arr2 =  [0, 0, 1, 1]
 
    # Size of the array
    N = len(arr1)
     
    # Function Call
    canMakeEqual(arr1, arr2, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
  // Function to check if arr1[] can be
  // converted to arr2[] by swapping pair
  // (i, j) such that i < j and arr[i] is
  // 1 and arr[j] is 0
  static void canMakeEqual(int []arr1, int []arr2, int N)
  {
 
    // Stores the differences of prefix
    // sum of arr1 and arr2
    int count = 0;
 
    // Stores the count of 1 and
    // zero of arr1
    int arr1_one = 0, arr1_zero = 0;
 
    // Stores the count of 1 and
    // zero of arr2
    int arr2_one = 0, arr2_zero = 0;
 
    // Iterate in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
 
      // If arr1[i] is 1, then
      // increment arr1_one by one
      if (arr1[i] == 1) {
        arr1_one++;
      }
 
      // Otherwise increment
      // arr1_zero by one
      else if (arr1[i] == 0) {
        arr1_zero++;
      }
 
      // If arr2[i] is 1, then
      // increment arr2_one by one
      if (arr2[i] == 1) {
        arr2_one++;
      }
 
      // Otherwise increment
      // arr2_zero by one
      else if (arr2[i] == 0) {
        arr2_zero++;
      }
    }
 
    // Check if number of 1s and 0s
    // of arr1 is equal to number of
    // 1s and 0s of arr2 respectievly
    if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
      Console.WriteLine("No");
      return;
    }
 
    // Iterate over the range [0, N-1]
    for (int i = 0; i < N; i++) {
 
      // Increment count by differences
      // arr1[i] and arr2[i]
      count = count + (arr1[i] - arr2[i]);
 
      // Check if number of 1's in
      // arr2 are more than arr1 and
      // then print "No"
      if (count < 0) {
        Console.WriteLine("No");
        return;
      }
    }
 
    // Finally, print "Yes"
    Console.WriteLine("Yes");
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Given input arrays
    int []arr1 = { 0, 1, 1, 0 };
    int []arr2 = { 0, 0, 1, 1 };
 
    // Size of the array
    int N = arr1.Length;
 
    // Function Call
    canMakeEqual(arr1, arr2, N);
  }
 
}
 
// This code is contributed by bgangwar59.


Javascript


输出:
Yes

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

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