📜  使用给定的a和b单位可以交叉的最大元素

📅  最后修改于: 2021-04-24 14:41:30             🧑  作者: Mango

给定N个元素和两个初始值a和b的二进制数组。如果满足以下条件,我们可以越过第i个元素:

  1. 如果a [i] == 0 ,那么我们可以使用b或a中的1个单位穿过第i个元素。
  2. 如果a [i] == 1 ,则如果我们使用b中的1个单位,则a增加1个单位。如果从a使用1个单位,则a或b都不会增加。

任务是找到使用a和b单位可以交叉的最大元素数。

注意:如果我们将a任意增加1,则它不能超过a的原始值。

例子:

方法:迭代数组元素并执行以下步骤:

  • 如果没有b或a传递元素,则中断。
  • 否则,如果没有剩余,则使用b;如果arr [i] == 1,则将a加1。
  • 否则,如果没有b,则使用a。
  • 否则,如果arr [i] == 1,则使用b并将a增加1直到原始a的最大值。
  • 否则,只需使用a中的1个单位。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to find the number
// of elements crossed
int findElementsCrossed(int arr[], int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
  
    // Iterate in the binary array
    for (int i = 0; i < n; i++) {
  
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
  
        // If there is no a
        else if (a == 0) {
  
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1) {
                b -= 1;
                a = min(aa, a + 1);
            }
  
            // simply use b
            else
                b -= 1;
        }
  
        // Use a if theres no b
        else if (b == 0)
            a--;
  
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa) {
            b -= 1;
            a = min(aa, a + 1);
        }
  
        // Use a
        else
            a--;
        ans++;
    }
  
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 0, 0, 1, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int a = 1;
    int b = 2;
    cout << findElementsCrossed(arr, a, b, n);
  
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG
{
  
// Function to find the number
// of elements crossed
static int findElementsCrossed(int arr[], 
                        int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
  
    // Iterate in the binary array
    for (int i = 0; i < n; i++)
    {
  
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
  
        // If there is no a
        else if (a == 0)
        {
  
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1)
            {
                b -= 1;
                a = Math.min(aa, a + 1);
            }
  
            // simply use b
            else
                b -= 1;
        }
  
        // Use a if theres no b
        else if (b == 0)
            a--;
  
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa)
        {
            b -= 1;
            a = Math.min(aa, a + 1);
        }
  
        // Use a
        else
            a--;
        ans++;
    }
  
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 0, 0, 1, 0, 1 };
    int n = arr.length;
    int a = 1;
    int b = 2;
    System.out.println(findElementsCrossed(arr, a, b, n));
  
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 program to implement
# the above approach
  
# Function to find the number
# of elements crossed
def findElementsCrossed(arr, a, b, n):
  
    # Keep a copy of a
    aa = a
    ans = 0
  
    # Iterate in the binary array
    for i in range(n):
  
        # If no a and b left to use
        if (a == 0 and b == 0):
            break
  
        # If there is no a
        elif (a == 0):
  
            # use b and increase a by 1
            # if arr[i] is 1
            if (arr[i] == 1):
                b -= 1
                a = min(aa, a + 1)
              
            # simply use b
            else:
                b -= 1
          
        # Use a if theres no b
        elif (b == 0):
            a -= 1
  
        # Increase a and use b if arr[i] == 1
        elif (arr[i] == 1 and a < aa):
            b -= 1
            a = min(aa, a + 1)
          
        # Use a
        else:
            a -= 1
        ans += 1
      
    return ans
  
# Driver code
arr = [1, 0, 0, 1, 0, 1]
n = len(arr)
a = 1
b = 2
print(findElementsCrossed(arr, a, b, n))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the above approach 
using System;
  
class GFG
{
  
// Function to find the number
// of elements crossed
static int findElementsCrossed(int []arr, 
                        int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
  
    // Iterate in the binary array
    for (int i = 0; i < n; i++)
    {
  
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
  
        // If there is no a
        else if (a == 0)
        {
  
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1)
            {
                b -= 1;
                a = Math.Min(aa, a + 1);
            }
  
            // simply use b
            else
                b -= 1;
        }
  
        // Use a if theres no b
        else if (b == 0)
            a--;
  
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa)
        {
            b -= 1;
            a = Math.Min(aa, a + 1);
        }
  
        // Use a
        else
            a--;
        ans++;
    }
  
    return ans;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 0, 0, 1, 0, 1 };
    int n = arr.Length;
    int a = 1;
    int b = 2;
    Console.WriteLine(findElementsCrossed(arr, a, b, n));
  
}
}
  
// This code contributed by Rajput-Ji


PHP


输出:
3