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

📅  最后修改于: 2021-04-17 17:28:55             🧑  作者: Mango

给定两个阵列ARR1 []ARR2 [大小为N],由二进制整数的,任务是,以检查是否ARR1 []可以通过交换任何对的数组元素的(ARR1转换为ARR2 [] [I]中,ARR1 [ j]) ,使得i arr1 [i]1arr1 [j]0 (任意次数)。如果可能的话,请打印“是” 。否则,打印“否”

例子:

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

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

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

  • 初始化一个变量,比如说count0 ,以存储arr1 []arr2 []的前缀和的差。
  • 1 和在两个阵列和校验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.


输出:
Yes

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