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++ program to implement // the above approach #include <bits/stdc++.h> 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 there is 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 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 there is 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 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 there is 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# 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 there is 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 // PHP program to implement // the above approach // Function to find the number // of elements crossed function findElementsCrossed( $arr , $a , $b , $n )
{ // Keep a copy of a
$aa = $a ;
$ans = 0;
// Iterate in the binary array
for ( $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 there is 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 $arr = array (1, 0, 0, 1, 0, 1);
$n = sizeof( $arr );
$a = 1;
$b = 2;
echo findElementsCrossed( $arr , $a , $b , $n );
// This code is contributed by Akanksha Rai ?> |
<script> // javascript program to implement // the above approach // Function to find the number
// of elements crossed
function findElementsCrossed(arr , a , b , n) {
// Keep a copy of a
var aa = a;
var ans = 0;
// Iterate in the binary array
for (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 there is 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
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));
// This code contributed by umadevi9616 </script> |
3
Time Complexity: O(n), to iterate over the array where n is the size of the array
Auxiliary Space: O(1)