Maximum elements which can be crossed using given units of a and b
Given a binary array of N elements and two initial values a and b. We can cross the i-th element if:
- If a[i] == 0, then we can use 1 unit from either of b or a to cross the i-th element.
- If a[i] == 1, then if we use 1 unit from b, a increases by 1 unit. In case 1 unit is used from a, then there is no increase in either of a or b.
The task is to find the maximum number of elements that can be crossed using a and b units.
Note: When we increase a by 1 at any step, it cannot exceed the original value of a.
Examples:
Input: arr[] = {0, 1, 0, 1, 0}, a = 1, b = 2;
Output: 5
Use 1 unit from a to cross 1st element. (a = 0 and b = 2)
Use 1 unit from b to cross 2nd element. (a = 1 and b = 1)
Use 1 unit from a to cross 3rd element. (a = 0 and b = 1)
Use 1 unit from b to cross 4th element. (a = 1 and b = 0)
Use 1 unit from a to cross 5th element. (a = 0 and b = 0)
Input: a[] = {1, 0, 0, 1, 0, 1}, a = 1, b = 2
Use 1 unit from b to cross first element. (a = 1 and b = 1)
Use 1 unit from b to cross second element. (a = 1 and b = 0)
Use 1 unit from a to cross third element. (a = 0 and b = 0)
Output: 3
Approach: Iterate in the array element and perform the following steps:
- Break if we do not have either of b or a to pass the element.
- Else, use b if there is no a left, and increase a by 1 if arr[i] == 1.
- Else, use a if there is no b left.
- Else, use b if arr[i]==1 and increase a by 1 till the maximum of the original a.
- Else, simply use 1 unit from a.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findElementsCrossed( int arr[], int a, int b, int n)
{
int aa = a;
int ans = 0;
for ( int i = 0; i < n; i++) {
if (a == 0 && b == 0)
break ;
else if (a == 0) {
if (arr[i] == 1) {
b -= 1;
a = min(aa, a + 1);
}
else
b -= 1;
}
else if (b == 0)
a--;
else if (arr[i] == 1 && a < aa) {
b -= 1;
a = min(aa, a + 1);
}
else
a--;
ans++;
}
return ans;
}
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
import java.util.*;
class GFG
{
static int findElementsCrossed( int arr[],
int a, int b, int n)
{
int aa = a;
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a == 0 && b == 0 )
break ;
else if (a == 0 )
{
if (arr[i] == 1 )
{
b -= 1 ;
a = Math.min(aa, a + 1 );
}
else
b -= 1 ;
}
else if (b == 0 )
a--;
else if (arr[i] == 1 && a < aa)
{
b -= 1 ;
a = Math.min(aa, a + 1 );
}
else
a--;
ans++;
}
return ans;
}
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));
}
}
|
Python3
def findElementsCrossed(arr, a, b, n):
aa = a
ans = 0
for i in range (n):
if (a = = 0 and b = = 0 ):
break
elif (a = = 0 ):
if (arr[i] = = 1 ):
b - = 1
a = min (aa, a + 1 )
else :
b - = 1
elif (b = = 0 ):
a - = 1
elif (arr[i] = = 1 and a < aa):
b - = 1
a = min (aa, a + 1 )
else :
a - = 1
ans + = 1
return ans
arr = [ 1 , 0 , 0 , 1 , 0 , 1 ]
n = len (arr)
a = 1
b = 2
print (findElementsCrossed(arr, a, b, n))
|
C#
using System;
class GFG
{
static int findElementsCrossed( int []arr,
int a, int b, int n)
{
int aa = a;
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (a == 0 && b == 0)
break ;
else if (a == 0)
{
if (arr[i] == 1)
{
b -= 1;
a = Math.Min(aa, a + 1);
}
else
b -= 1;
}
else if (b == 0)
a--;
else if (arr[i] == 1 && a < aa)
{
b -= 1;
a = Math.Min(aa, a + 1);
}
else
a--;
ans++;
}
return ans;
}
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));
}
}
|
PHP
<?php
function findElementsCrossed( $arr , $a , $b , $n )
{
$aa = $a ;
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a == 0 && $b == 0)
break ;
else if ( $a == 0)
{
if ( $arr [ $i ] == 1)
{
$b -= 1;
$a = min( $aa , $a + 1);
}
else
$b -= 1;
}
else if ( $b == 0)
$a --;
else if ( $arr [ $i ] == 1 && $a < $aa )
{
$b -= 1;
$a = min( $aa , $a + 1);
}
else
$a --;
$ans ++;
}
return $ans ;
}
$arr = array (1, 0, 0, 1, 0, 1);
$n = sizeof( $arr );
$a = 1;
$b = 2;
echo findElementsCrossed( $arr , $a , $b , $n );
?>
|
Javascript
<script>
function findElementsCrossed(arr , a , b , n) {
var aa = a;
var ans = 0;
for (i = 0; i < n; i++) {
if (a == 0 && b == 0)
break ;
else if (a == 0) {
if (arr[i] == 1) {
b -= 1;
a = Math.min(aa, a + 1);
}
else
b -= 1;
}
else if (b == 0)
a--;
else if (arr[i] == 1 && a < aa) {
b -= 1;
a = Math.min(aa, a + 1);
}
else
a--;
ans++;
}
return ans;
}
var arr = [ 1, 0, 0, 1, 0, 1 ];
var n = arr.length;
var a = 1;
var b = 2;
document.write(findElementsCrossed(arr, a, b, n));
</script>
|
Time Complexity: O(n), to iterate over the array where n is the size of the array
Auxiliary Space: O(1)
Last Updated :
09 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...