📌  相关文章
📜  最低数量使所有数组元素为零所需的操作

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

最低数量使所有数组元素为零所需的操作

给定一个包含 N 个元素的数组,每个元素为 1 或 0。您需要通过执行以下操作使数组的所有元素都等于 0:

  • 如果一个元素为 1,您可以将其值更改为 0,然后,
    • 如果下一个连续元素是 1,它将自动转换为 0。
    • 如果下一个连续元素已经为 0,则什么也不会发生。

现在,任务是找到使所有元素等于 0 所需的最小操作数。
例子

Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 0, 1} 
Output : Minimum changes: 3 

Input : arr[] = {1, 1, 1, 1}
Output : Minimum changes: 1 

方法一:  

要找到所需的最小更改次数,请从左到右迭代数组并检查当前元素是否为 1。如果当前元素为 1,则将其更改为 0,并将计数加 1,并搜索 0 进行下一次操作,因为所有连续的 1 将自动转换为 0。

时间复杂度: O(n 2 )

以下是上述方法的实现:

C++
// CPP program to find  minimum number of
// operations required to make all
// array elements zero
 
#include 
using namespace std;
 
// Function to find  minimum number of
// operations required to make all
// array elements zero
int minimumChanges(int arr[], int n)
{
    int i;
     
    // It will maintain total changes required
    int changes = 0;
     
    for (i = 0; i < n; i++)
    {  
        // Check for the first 1
        if (arr[i] == 1)
        {  
            int j;
             
            // Check for number of
            // consecutive 1's
            for(j = i+1; j


Java
// Java program to find minimum
// number of operations required
// to make all array elements zero
 
class GFG
{
 
// Function to find minimum number
// of operations required to make 
// all array elements zero
static int minimumChanges(int arr[],
                          int n)
{
    int i;
     
    // It will maintain total
    // changes required
    int changes = 0;
     
    for (i = 0; i < n; i++)
    {
        // Check for the first 1
        if (arr[i] == 1)
        {
            int j;
             
            // Check for number of
            // consecutive 1's
            for(j = i + 1; j < n; j++)
            {
                if(arr[j] == 0)
                    break;
            }
             
            // Increment i to the position 
            // of last consecutive 1
            i = j - 1;
             
            changes++;
        }
    }
     
    return changes;
}
 
// Driver code
public static void main (String args[])
{
    int arr[] = { 1, 1, 0, 0, 0,
                     1, 0, 1, 1 };
    int n = arr.length ;
     
    System.out.println("Minimum operations: " +
                        minimumChanges(arr, n));
     
}
}
 
// This code is contributed by ANKITRAI1


Python 3
# Python 3 program to find
# minimum number of operations
# required to make all array
# elements zero
 
# Function to find minimum number
# of operations required to make
# all array elements zero
def minimumChanges(arr, n) :
 
    # It will maintain total
    # changes required
    changes = 0
     
    i = 0
     
    while i < n :
 
        # Check for the first 1
        if arr[i] == 1 :
 
            j = i + 1
 
            # Check for number of
            # consecutive 1's
            while j < n:
 
                if arr[j] == 0 :
                    break
 
                j += 1
 
            # Increment i to the position
            # of last consecutive 1
            i = j - 1
             
            changes += 1
 
        i += 1
         
    return changes
 
# Driver code    
if __name__ == "__main__" :
 
    arr = [ 1, 1, 0, 0, 0, 1, 0, 1, 1]
    n = len(arr)
 
    print("Minimum operations:",
           minimumChanges(arr, n))
 
# This code is contributed by ANKITRAI1


C#
// C# program to find minimum
// number of operations required
// to make all array elements zero
class GFG
{
 
// Function to find minimum number
// of operations required to make
// all array elements zero
static int minimumChanges(int[] arr,
                          int n)
{
    int i;
     
    // It will maintain total
    // changes required
    int changes = 0;
     
    for (i = 0; i < n; i++)
    {
        // Check for the first 1
        if (arr[i] == 1)
        {
            int j;
             
            // Check for number of
            // consecutive 1's
            for(j = i + 1; j < n; j++)
            {
                if(arr[j] == 0)
                    break;
            }
             
            // Increment i to the position
            // of last consecutive 1
            i = j - 1;
             
            changes++;
        }
    }
     
    return changes;
}
 
// Driver code
static void Main()
{
    int[] arr = new int[]{ 1, 1, 0, 0, 0,
                           1, 0, 1, 1 };
    int n = arr.Length ;
     
    System.Console.WriteLine("Minimum operations: " +
                             minimumChanges(arr, n));
}
}
 
// This code is contributed by mits


PHP


Javascript


C++
// CPP program to find minimum number of
// operations required to make all
// array elements zero
 
#include 
using namespace std;
 
// Function to find minimum number of
// operations required to make all
// array elements zero
int minimumChanges(int arr[], int n)
{
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n-1; i++) {
       
        // If we there is a consecutive pair of '1' and
        // '0'(in respective order)
        if ((arr[i] == 1) && (arr[i + 1] == 0)) {
           
            // We increment our returning variable by 1
            changes++;
        }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
        changes++;
       
        // If it is '1', we again increment our
        // returning variable by 1
    }
 
    return changes;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Minimum operations: "
         << minimumChanges(arr, n);
 
    return 0;
}
 
// This code is contributed by yashbro


Java
// Java program to find minimum number of
// operations required to make all
// array elements zero
class GFG
{
 
  // Function to find minimum number of
  // operations required to make all
  // array elements zero
  public static int minimumChanges(int arr[], int n)
  {
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n-1; i++) {
 
      // If we there is a consecutive pair of '1' and
      // '0'(in respective order)
      if ((arr[i] == 1) && (arr[i + 1] == 0)) {
 
        // We increment our returning variable by 1
        changes++;
      }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
      changes++;
 
      // If it is '1', we again increment our
      // returning variable by 1
    }
 
    return changes;
  }
 
  // Driver code
 
  public static void main(String[] args)
  {
    int arr[] = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = arr.length;
 
    System.out.println( "Minimum operations: "+ minimumChanges(arr, n));
 
  }
}
 
//This code is contributed by sravan kumar


Python3
# Python 3 program to find
# minimum number of operations
# required to make all array
# elements zero
 
# Function to find minimum number
# of operations required to make
# all array elements zero
 
 
def minimumChanges(arr, n):
    # It will maintain total
    # changes required
    changes = 0
 
    # We iterate from 0 to n-1
    # We can't iterate from 0 to n as the arr[i+1] will be out of index
    for i in range(n - 1):
 
        # If we there is a consecutive pair of '1' and '0'(in respective order)
        if arr[i] == 1 and arr[i + 1] == 0:
            # We increment our returning variable by 1
            changes += 1
 
    # After the loop ends, we check the last element whether it is '1'
    if arr[n - 1] == 1:
        changes += 1  # If it is '1', we again increment our returning variable by 1
 
    return changes
 
 
# Driver code
if __name__ == "__main__":
    arr = [1, 1, 0, 0, 0, 1, 0, 1, 1]
    n = len(arr)
 
    print("Minimum operations:",
          minimumChanges(arr, n))
 
# This code is contributed by yashbro


C#
// C# program to find minimum number of
// operations required to make all
// array elements zero
using System;
class GFG
{
 
  // Function to find minimum number of
  // operations required to make all
  // array elements zero
  static int minimumChanges(int[] arr, int n)
  {
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n - 1; i++) {
 
      // If we there is a consecutive pair of '1' and
      // '0'(in respective order)
      if ((arr[i] == 1) && (arr[i + 1] == 0)) {
 
        // We increment our returning variable by 1
        changes++;
      }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
      changes++;
 
      // If it is '1', we again increment our
      // returning variable by 1
    }
 
    return changes;
  }
 
  // Driver code
  public static int Main()
  {
    int[] arr = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = arr.Length;
 
    Console.Write("Minimum operations: "
                  + minimumChanges(arr, n));
    return 0;
  }
}
// This code is contributed by Taranpreet


Javascript


输出:
Minimum operations: 3

方法二:  

  1. 正如我们已经知道的,我们必须寻找“1”的连续组/簇,因为在更改组的第一个“1”后,其余的连续“1”将自动更改。所以要找到连续的“1”,我们可以遍历数组并找到第一个。 '1' 和 '0' 的连续对,因为它将指示连续 '1' 的断点。
  2. 在最后一个索引处,我们将检查数组的最后一个元素是“1”还是“0”,因为如果它是“1”,那么可能存在连续的“1”组,因此迭代结束时,我们的循环找不到断点。

下面是上述方法的实现:

C++

// CPP program to find minimum number of
// operations required to make all
// array elements zero
 
#include 
using namespace std;
 
// Function to find minimum number of
// operations required to make all
// array elements zero
int minimumChanges(int arr[], int n)
{
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n-1; i++) {
       
        // If we there is a consecutive pair of '1' and
        // '0'(in respective order)
        if ((arr[i] == 1) && (arr[i + 1] == 0)) {
           
            // We increment our returning variable by 1
            changes++;
        }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
        changes++;
       
        // If it is '1', we again increment our
        // returning variable by 1
    }
 
    return changes;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Minimum operations: "
         << minimumChanges(arr, n);
 
    return 0;
}
 
// This code is contributed by yashbro

Java

// Java program to find minimum number of
// operations required to make all
// array elements zero
class GFG
{
 
  // Function to find minimum number of
  // operations required to make all
  // array elements zero
  public static int minimumChanges(int arr[], int n)
  {
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n-1; i++) {
 
      // If we there is a consecutive pair of '1' and
      // '0'(in respective order)
      if ((arr[i] == 1) && (arr[i + 1] == 0)) {
 
        // We increment our returning variable by 1
        changes++;
      }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
      changes++;
 
      // If it is '1', we again increment our
      // returning variable by 1
    }
 
    return changes;
  }
 
  // Driver code
 
  public static void main(String[] args)
  {
    int arr[] = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = arr.length;
 
    System.out.println( "Minimum operations: "+ minimumChanges(arr, n));
 
  }
}
 
//This code is contributed by sravan kumar

Python3

# Python 3 program to find
# minimum number of operations
# required to make all array
# elements zero
 
# Function to find minimum number
# of operations required to make
# all array elements zero
 
 
def minimumChanges(arr, n):
    # It will maintain total
    # changes required
    changes = 0
 
    # We iterate from 0 to n-1
    # We can't iterate from 0 to n as the arr[i+1] will be out of index
    for i in range(n - 1):
 
        # If we there is a consecutive pair of '1' and '0'(in respective order)
        if arr[i] == 1 and arr[i + 1] == 0:
            # We increment our returning variable by 1
            changes += 1
 
    # After the loop ends, we check the last element whether it is '1'
    if arr[n - 1] == 1:
        changes += 1  # If it is '1', we again increment our returning variable by 1
 
    return changes
 
 
# Driver code
if __name__ == "__main__":
    arr = [1, 1, 0, 0, 0, 1, 0, 1, 1]
    n = len(arr)
 
    print("Minimum operations:",
          minimumChanges(arr, n))
 
# This code is contributed by yashbro

C#

// C# program to find minimum number of
// operations required to make all
// array elements zero
using System;
class GFG
{
 
  // Function to find minimum number of
  // operations required to make all
  // array elements zero
  static int minimumChanges(int[] arr, int n)
  {
    int i;
 
    // It will maintain total changes
    // required and return as
    // answer
    int changes = 0;
 
    // We iterate from 0 to n-1
    // We can't iterate from 0 to n as
    // the arr[i+1] will be
    // out of index
    for (i = 0; i < n - 1; i++) {
 
      // If we there is a consecutive pair of '1' and
      // '0'(in respective order)
      if ((arr[i] == 1) && (arr[i + 1] == 0)) {
 
        // We increment our returning variable by 1
        changes++;
      }
    }
 
    // After the loop ends, we check the last element
    // whether it is '1'
    if (arr[n - 1] == 1) {
      changes++;
 
      // If it is '1', we again increment our
      // returning variable by 1
    }
 
    return changes;
  }
 
  // Driver code
  public static int Main()
  {
    int[] arr = { 1, 1, 0, 0, 0, 1, 0, 1, 1 };
    int n = arr.Length;
 
    Console.Write("Minimum operations: "
                  + minimumChanges(arr, n));
    return 0;
  }
}
// This code is contributed by Taranpreet

Javascript


输出
Minimum operations: 3

时间复杂度: O(N)