📌  相关文章
📜  如果连续三元组在给定的二进制数组中可被 3 整除,则通过删除中间元素查找 0 是否被删除更多或 1

📅  最后修改于: 2022-05-13 01:56:06.620000             🧑  作者: Mango

如果连续三元组在给定的二进制数组中可被 3 整除,则通过删除中间元素查找 0 是否被删除更多或 1

给定一个大小为N10 的二进制数组a[] 。如果a[i-1]+a[i]+a[i+1]可被3整除,则任务是删除一个元素。如果删除的1数量多于0则打印1 ,否则打印0

例子:

方法:这个想法是基于这样的观察,如果两个邻居都等于当前元素,因为如果(a[i]=a[i-1]=a[i+1])那么总和总是可以被3整除。假设A存储删除的1 的数量, B存储删除的0 的数量。如果第 i 个元素等于邻居,则如果a[i] =1 ,则增加A ,否则增加B 。如果A的计数大于B ,则打印1 ,否则打印2=0 。请按照以下步骤解决问题:

  • 将变量AB初始化为0以存储移除的10的个数。
  • 使用变量i迭代范围[0, N)并执行以下步骤:
    • 如果a[i-1]、a[i]a[i+1]相等,则如果a[i]等于1,A的值加1 ,否则B1。
  • 如果A大于B,则打印1 ,否则打印0。

下面是上述方法的实现。

C++
#include 
using namespace std;
 
// Function to if more number of
// 1's are removed or 0's
void solution(vector a)
{
 
    // Stores count of 1's removes
    int A = 0;
 
    // Stores count of 0's removes
    int B = 0;
 
    // Traverse the array
    for (int i = 1; i < a.size() - 1; i++) {
 
        // Check the divisibility
        if (a[i] == a[i - 1] && a[i] == a[i + 1]) {
 
            // Check for 1 or 0
            if (a[i] == 1)
                A++;
            else
                B++;
        }
    }
 
    // Print the result
    if (A > B)
        cout << ("1");
    else
        cout << ("0");
}
 
// Driver Code
int main()
{
 
    vector a = { 1, 1, 1, 0, 1, 0, 0 };
    solution(a);
    return 0;
}
 
// This code is contributed by lokeshpotta20.


Java
// Java program for above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to if more number of
    // 1's are removed or 0's
    public static void solution(int[] a)
    {
 
        // Stores count of 1's removes
        int A = 0;
 
        // Stores count of 0's removes
        int B = 0;
 
        // Traverse the array
        for (int i = 1; i < a.length - 1; i++) {
 
            // Check the divisibility
            if (a[i] == a[i - 1]
                && a[i] == a[i + 1]) {
 
                // Check for 1 or 0
                if (a[i] == 1)
                    A++;
                else
                    B++;
            }
        }
 
        // Print the result
        if (A > B)
            System.out.println("1 ");
        else
            System.out.println("0 ");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int a[] = { 1, 1, 1, 0, 1, 0, 0 };
        solution(a);
    }
}


Python3
# Python3 program for above approach
 
 
# Function to if more number of
# 1's are removed or 0's
def solution(a) :
 
    # Stores count of 1's removes
    A = 0;
 
    # Stores count of 0's removes
    B = 0;
 
    # Traverse the array
    for i in range(1 , len(a)- 1) :
 
        # Check the divisibility
        if (a[i] == a[i - 1] and a[i] == a[i + 1]) :
 
            # Check for 1 or 0
            if (a[i] == 1) :
                A += 1;
            else :
                B += 1;
 
    # Print the result
    if (A > B) :
        print("1");
    else :
        print("0");
 
# Driver Code
if __name__ == "__main__" :
 
 
    a = [ 1, 1, 1, 0, 1, 0, 0 ];
    solution(a);
 
    # This code is contributed by AnkThon


C#
// C# program for above approach
using System;
public class GFG {
 
    // Function to if more number of
    // 1's are removed or 0's
    public static void solution(int[] a)
    {
 
        // Stores count of 1's removes
        int A = 0;
 
        // Stores count of 0's removes
        int B = 0;
 
        // Traverse the array
        for (int i = 1; i < a.Length - 1; i++) {
 
            // Check the divisibility
            if (a[i] == a[i - 1]
                && a[i] == a[i + 1]) {
 
                // Check for 1 or 0
                if (a[i] == 1)
                    A++;
                else
                    B++;
            }
        }
 
        // Print the result
        if (A > B)
            Console.WriteLine("1 ");
        else
            Console.WriteLine("0 ");
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int []a = { 1, 1, 1, 0, 1, 0, 0 };
        solution(a);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出
1 

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