📌  相关文章
📜  通过删除三元组将给定的二进制数组减少为单个元素

📅  最后修改于: 2021-09-04 11:24:44             🧑  作者: Mango

给定一个二进制数组arr[] cof size N ,任务是通过以下两个操作将数组缩减为单个元素:

  • 连续的01的三元组保持不变。
  • 两个 0 和单个 1组成的连续数组元素的三元组可以转换为更频繁的元素,反之亦然

例子:

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

  • 计算01的频率。
  • 计算它们各自计数的绝对差。
  • 如果差为 1,则数组只能减少为 1。因此,打印 Yes。
  • 否则,打印编号。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if it is possible to
// reduce the array to a single element
void solve(int arr[], int n)
{
    // Stores frequency of 0's
    int countzeroes = 0;
 
    // Stores frequency of 1's
    int countones = 0;
 
    for (int i = 0; i < n; i++) {
 
        if (arr[i] == 0)
            countzeroes++;
        else
            countones++;
    }
 
    // Condition for array to be reduced
    if (abs(countzeroes - countones) == 1)
        cout << "Yes";
 
    // Otherwise
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 0, 0, 1, 1, 1 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    solve(arr, n);
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to check if it is possible to
// reduce the array to a single element
static void solve(int arr[], int n)
{
     
    // Stores frequency of 0's
    int countzeroes = 0;
 
    // Stores frequency of 1's
    int countones = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (arr[i] == 0)
            countzeroes++;
        else
            countones++;
    }
 
    // Condition for array to be reduced
    if (Math.abs(countzeroes - countones) == 1)
        System.out.print("Yes");
 
    // Otherwise
    else
        System.out.print("No");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 0, 1, 0, 0, 1, 1, 1 };
 
    int n = arr.length;
 
    solve(arr, n);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to check if it is possible to
# reduce the array to a single element
def solve(arr, n):
 
    # Stores frequency of 0's
    countzeroes = 0;
 
    # Stores frequency of 1's
    countones = 0;
 
    for i in range(n):
        if (arr[i] == 0):
            countzeroes += 1;
        else:
            countones += 1;
     
    # Condition for array to be reduced
    if (abs(countzeroes - countones) == 1):
        print("Yes");
 
    # Otherwise
    else:
        print("No");
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 0, 1, 0, 0, 1, 1, 1 ];
 
    n = len(arr);
 
    solve(arr, n);
 
# This code is contributed by Amit Katiyar


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check if it is possible to
// reduce the array to a single element
static void solve(int []arr, int n)
{
     
    // Stores frequency of 0's
    int countzeroes = 0;
 
    // Stores frequency of 1's
    int countones = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (arr[i] == 0)
            countzeroes++;
        else
            countones++;
    }
 
    // Condition for array to be reduced
    if (Math.Abs(countzeroes - countones) == 1)
        Console.Write("Yes");
 
    // Otherwise
    else
        Console.Write("No");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 0, 1, 0, 0, 1, 1, 1 };
 
    int n = arr.Length;
 
    solve(arr, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Yes

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