📌  相关文章
📜  从二进制数组中相邻的0的左侧删除所有1

📅  最后修改于: 2021-04-24 22:21:54             🧑  作者: Mango

给定一个二进制数组arr [] ,任务是找到从相邻的0左侧删除所有1所需的操作数。在每个操作中,紧邻0的所有1均变为0。

例子:

方法:此问题可以使用贪婪方法解决。这个想法是要计算0之前连续1的最大数目,该数目给出了执行给定操作所需的次数,以使数组无法进一步更改。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to find the maximum number
// of 1's before 0
void noOfMoves(int arr[], int n)
{
    int cnt = 0;
    int maxCnt = 0;
  
    // Traverse the array
    for (int i = 0; i < n; i++) {
  
        // If value is 1
        if (arr[i] == 1) {
            cnt++;
        }
        else {
  
            // If consecutive 1 followed
            // by 0, then update the maxCnt
            if (cnt != 0) {
                maxCnt = max(maxCnt, cnt);
                cnt = 0;
            }
        }
    }
  
    // Print the maximum consecutive 1's
    // followed by 0
    cout << maxCnt << endl;
}
  
// Driver Code
int main()
{
    int arr[] = { 0, 1, 1, 1, 1, 0,
                  0, 1, 1, 0, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    noOfMoves(arr, N);
    int arr1[] = { 1, 0, 1, 0, 1, 0, 1, 0 };
    N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    noOfMoves(arr1, N);
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
  
// Function to find the maximum number
// of 1's before 0
static void noOfMoves(int arr[], int n)
{
    int cnt = 0;
    int maxCnt = 0;
  
    // Traverse the array
    for (int i = 0; i < n; i++) {
  
        // If value is 1
        if (arr[i] == 1) {
            cnt++;
        }
        else {
  
            // If consecutive 1 followed
            // by 0, then update the maxCnt
            if (cnt != 0) {
                maxCnt = Math.max(maxCnt, cnt);
                cnt = 0;
            }
        }
    }
  
    // Print the maximum consecutive 1's
    // followed by 0
    System.out.print(maxCnt +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 0, 1, 1, 1, 1, 0,
                0, 1, 1, 0, 0, 1 };
    int N = arr.length;
  
    // Function Call
    noOfMoves(arr, N);
    int arr1[] = { 1, 0, 1, 0, 1, 0, 1, 0 };
    N = arr1.length;
  
    // Function Call
    noOfMoves(arr1, N);
}
}
  
// This code is contributed by 29AjayKumar


Python 3
# Python 3 implementation of the above approach
  
# Function to find the maximum number
# of 1's before 0
def noOfMoves(arr,n):
    cnt = 0
    maxCnt = 0
  
    # Traverse the array
    for i in range(n):
        # If value is 1
        if (arr[i] == 1):
            cnt += 1
        else:
            # If consecutive 1 followed
            # by 0, then update the maxCnt
            if (cnt != 0):
                maxCnt = max(maxCnt, cnt)
                cnt = 0
  
    # Print the maximum consecutive 1's
    # followed by 0
    print(maxCnt)
  
# Driver Code
if __name__ == '__main__':
    arr = [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1]
    N = len(arr)
  
    # Function Call
    noOfMoves(arr, N)
    arr1 = [1, 0, 1, 0, 1, 0, 1, 0]
    N = len(arr1)
  
    # Function Call
    noOfMoves(arr1, N)
  
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
  
public class GFG{
   
// Function to find the maximum number
// of 1's before 0
static void noOfMoves(int []arr, int n)
{
    int cnt = 0;
    int maxCnt = 0;
   
    // Traverse the array
    for (int i = 0; i < n; i++) {
   
        // If value is 1
        if (arr[i] == 1) {
            cnt++;
        }
        else {
   
            // If consecutive 1 followed
            // by 0, then update the maxCnt
            if (cnt != 0) {
                maxCnt = Math.Max(maxCnt, cnt);
                cnt = 0;
            }
        }
    }
   
    // Print the maximum consecutive 1's
    // followed by 0
    Console.Write(maxCnt +"\n");
}
   
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 0, 1, 1, 1, 1, 0,
                0, 1, 1, 0, 0, 1 };
    int N = arr.Length;
   
    // Function Call
    noOfMoves(arr, N);
    int []arr1 = { 1, 0, 1, 0, 1, 0, 1, 0 };
    N = arr1.Length;
   
    // Function Call
    noOfMoves(arr1, N);
}
}
  
// This code contributed by Rajput-Ji


输出:
4
1

时间复杂度: O(N) ,其中N是数组的长度。