Minimum number of days in which no task is performed
Given an array arr[] consisting of values (0, 1, 2, 3) of length N, representing the type of work that can be done on any ith day, such that the task is of either type A or type B. Each value in array is defined as:
0 – No tasks are available.
1 – Task of type B is available.
2 – Task of type A is available.
3 – Both task A and task B are available.
If the same type of task cannot be done for two consecutive days, the task is to minimize the number of days in which no task will be performed.
Examples:
Input: N = 4, arr = {0, 1, 3, 2}
Output : 2
Explanation : Following are the types of tasks done in each day.
On the 1st day there is no tasks so he does nothing and count becomes 1.
On the 2nd day, he performs task of type B.
On the 3rd day there are both tasks but as he had done task of type B on the previous day so he performs task A.
On the last day there is task A available but he had done the same task on the previous day so he does nothing and count becomes 2.
Therefore, 2 is the final answer.
Input: N = 8, arr[] = {0, 1, 3, 2, 0, 2, 3, 3}
Output: 3
Naive approach: This problem can be solved by using recursion. Follow the steps below to solve the given problem.
- Declare a variable say count = 0, to store the answer.
- If arr[i]=0, no task is there so do nothing and increase the count.
- If arr[i]=1, only task B is available, if the last day’s task was B then do nothing and increase the count else perform task B.
- If arr[i]=2, only task A is available, if the last day’s task was A then do nothing and increase the count else perform task A.
- If arr[i]=3, and the last day’s task was A then perform task B, if the last day’s task was B then perform task A, else perform the task which will minimize the number of days in which no task is performed.
- Use a variable last to keep track of the previous day’s task, which can take the following values :
- 0 – no task performed.
- 1 – the task of type A performed
- 2 – the task of type B performed
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int a[], int last, int n)
{
if (n == 0)
return 0;
if (a[n - 1] == 0) {
return 1 + solve(a, 0, n - 1);
}
else if (a[n - 1] == 1) {
if (last == 2)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 2, n - 1);
}
else if (a[n - 1] == 2) {
if (last == 1)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 1, n - 1);
}
else {
if (last == 1)
return solve(a, 2, n - 1);
else if (last == 2)
return solve(a, 1, n - 1);
else
return min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
int main()
{
int N = 4;
int arr[] = { 0, 1, 3, 2 };
cout << solve(arr, 0, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int solve( int []a, int last, int n)
{
if (n == 0 )
return 0 ;
if (a[n - 1 ] == 0 ) {
return 1 + solve(a, 0 , n - 1 );
}
else if (a[n - 1 ] == 1 ) {
if (last == 2 )
return 1 + solve(a, 0 , n - 1 );
else
return solve(a, 2 , n - 1 );
}
else if (a[n - 1 ] == 2 ) {
if (last == 1 )
return 1 + solve(a, 0 , n - 1 );
else
return solve(a, 1 , n - 1 );
}
else {
if (last == 1 )
return solve(a, 2 , n - 1 );
else if (last == 2 )
return solve(a, 1 , n - 1 );
else
return Math.min(solve(a, 2 , n - 1 ),
solve(a, 1 , n - 1 ));
}
}
public static void main(String args[])
{
int N = 4 ;
int []arr = { 0 , 1 , 3 , 2 };
System.out.println(solve(arr, 0 , N));
}
}
|
Python3
def solve(a, last, n):
if (n = = 0 ):
return 0
if (a[n - 1 ] = = 0 ):
return 1 + solve(a, 0 , n - 1 )
elif (a[n - 1 ] = = 1 ):
if (last = = 2 ):
return 1 + solve(a, 0 , n - 1 )
else :
return solve(a, 2 , n - 1 )
elif (a[n - 1 ] = = 2 ):
if (last = = 1 ):
return 1 + solve(a, 0 , n - 1 )
else :
return solve(a, 1 , n - 1 )
else :
if (last = = 1 ):
return solve(a, 2 , n - 1 )
elif (last = = 2 ):
return solve(a, 1 , n - 1 )
else :
return min (solve(a, 2 , n - 1 ),
solve(a, 1 , n - 1 ))
N = 4
arr = [ 0 , 1 , 3 , 2 ]
print (solve(arr, 0 , N))
|
C#
using System;
class GFG
{
static int solve( int []a, int last, int n)
{
if (n == 0)
return 0;
if (a[n - 1] == 0) {
return 1 + solve(a, 0, n - 1);
}
else if (a[n - 1] == 1) {
if (last == 2)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 2, n - 1);
}
else if (a[n - 1] == 2) {
if (last == 1)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 1, n - 1);
}
else {
if (last == 1)
return solve(a, 2, n - 1);
else if (last == 2)
return solve(a, 1, n - 1);
else
return Math.Min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
public static void Main()
{
int N = 4;
int []arr = { 0, 1, 3, 2 };
Console.Write(solve(arr, 0, N));
}
}
|
Javascript
<script>
function solve(a, last, n)
{
if (n == 0)
return 0;
if (a[n - 1] == 0) {
return 1 + solve(a, 0, n - 1);
}
else if (a[n - 1] == 1) {
if (last == 2)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 2, n - 1);
}
else if (a[n - 1] == 2) {
if (last == 1)
return 1 + solve(a, 0, n - 1);
else
return solve(a, 1, n - 1);
}
else {
if (last == 1)
return solve(a, 2, n - 1);
else if (last == 2)
return solve(a, 1, n - 1);
else
return Math.min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
let N = 4;
let arr = [ 0, 1, 3, 2 ];
document.write(solve(arr, 0, N));
</script>
|
Time Complexity: O(2n)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach Dynamic Programming can be used. Use memoization to store the previous state so that those previous states can be utilized to calculate further results.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dp[101][3];
int solve( int a[], int last, int n)
{
if (n == 0)
return 0;
if (dp[n][last] != -1)
return dp[n][last];
if (a[n - 1] == 0) {
return dp[n][last] = 1
+ solve(a, 0, n - 1);
}
else if (a[n - 1] == 1) {
if (last == 2)
return dp[n][last] = 1
+ solve(a, 0, n - 1);
else
return dp[n][last]
= solve(a, 2, n - 1);
}
else if (a[n - 1] == 2) {
if (last == 1)
return dp[n][last] = 1
+ solve(a, 0, n - 1);
else
return dp[n][last]
= solve(a, 1, n - 1);
}
else {
if (last == 1)
return dp[n][last]
= solve(a, 2, n - 1);
else if (last == 2)
return dp[n][last]
= solve(a, 1, n - 1);
else
return dp[n][last]
= min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
int main()
{
int N = 4;
int arr[] = { 0, 1, 3, 2 };
memset (dp, -1, sizeof (dp));
cout << solve(arr, 0, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int dp[][] = new int [ 101 ][ 3 ];
static int solve( int []a, int last, int n)
{
if (n == 0 )
return 0 ;
if (dp[n][last] != - 1 )
return dp[n][last];
if (a[n - 1 ] == 0 ) {
return dp[n][last] = 1
+ solve(a, 0 , n - 1 );
}
else if (a[n - 1 ] == 1 ) {
if (last == 2 )
return dp[n][last] = 1
+ solve(a, 0 , n - 1 );
else
return dp[n][last]
= solve(a, 2 , n - 1 );
}
else if (a[n - 1 ] == 2 ) {
if (last == 1 )
return dp[n][last] = 1
+ solve(a, 0 , n - 1 );
else
return dp[n][last]
= solve(a, 1 , n - 1 );
}
else {
if (last == 1 )
return dp[n][last]
= solve(a, 2 , n - 1 );
else if (last == 2 )
return dp[n][last]
= solve(a, 1 , n - 1 );
else
return dp[n][last]
= Math.min(solve(a, 2 , n - 1 ),
solve(a, 1 , n - 1 ));
}
}
public static void main(String args[])
{
int N = 4 ;
int []arr = { 0 , 1 , 3 , 2 };
for ( int i = 0 ; i < 101 ; i++) {
for ( int j = 0 ; j < 3 ; j++) {
dp[i][j] = - 1 ;
}
}
System.out.println(solve(arr, 0 , N));
}
}
|
Python3
dp = [ 0 ] * 101
def solve(a, last, n):
if (n = = 0 ):
return 0
if (dp[n][last] ! = - 1 ):
return dp[n][last]
if (a[n - 1 ] = = 0 ):
dp[n][last] = 1 + solve(a, 0 , n - 1 )
return dp[n][last]
elif (a[n - 1 ] = = 1 ):
if (last = = 2 ):
dp[n][last] = 1 + solve(a, 0 , n - 1 )
return dp[n][last]
else :
dp[n][last] = solve(a, 2 , n - 1 )
return dp[n][last]
elif (a[n - 1 ] = = 2 ):
if (last = = 1 ):
dp[n][last] = 1 + solve(a, 0 , n - 1 )
return dp[n][last]
else :
dp[n][last] = solve(a, 1 , n - 1 )
return dp[n][last]
else :
if (last = = 1 ):
dp[n][last] = solve(a, 2 , n - 1 )
return dp[n][last]
elif (last = = 2 ):
dp[n][last] = solve(a, 1 , n - 1 )
return dp[n][last]
else :
dp[n][last] = min (solve(a, 2 , n - 1 ),
solve(a, 1 , n - 1 ))
return dp[n][last]
N = 4
arr = [ 0 , 1 , 3 , 2 ]
for i in range ( len (dp)):
dp[i] = [ - 1 ] * 3
print (solve(arr, 0 , N))
|
C#
using System;
class GFG
{
static int [,]dp = new int [101, 3];
static int solve( int []a, int last, int n)
{
if (n == 0)
return 0;
if (dp[n, last] != -1)
return dp[n, last];
if (a[n - 1] == 0) {
return dp[n, last] = 1
+ solve(a, 0, n - 1);
}
else if (a[n - 1] == 1) {
if (last == 2)
return dp[n, last] = 1
+ solve(a, 0, n - 1);
else
return dp[n, last]
= solve(a, 2, n - 1);
}
else if (a[n - 1] == 2) {
if (last == 1)
return dp[n, last] = 1
+ solve(a, 0, n - 1);
else
return dp[n, last]
= solve(a, 1, n - 1);
}
else {
if (last == 1)
return dp[n, last]
= solve(a, 2, n - 1);
else if (last == 2)
return dp[n, last]
= solve(a, 1, n - 1);
else
return dp[n, last]
= Math.Min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
public static void Main()
{
int N = 4;
int []arr = { 0, 1, 3, 2 };
for ( int i = 0; i < 101; i++) {
for ( int j = 0; j < 3; j++) {
dp[i, j] = -1;
}
}
Console.Write(solve(arr, 0, N));
}
}
|
Javascript
<script>
let dp = new Array(101)
function solve(a, last, n)
{
if (n == 0)
return 0;
if (dp[n][last] != -1)
return dp[n][last];
if (a[n - 1] == 0) {
return dp[n][last] = 1
+ solve(a, 0, n - 1);
}
else if (a[n - 1] == 1)
{
if (last == 2)
return dp[n][last] = 1
+ solve(a, 0, n - 1);
else
return dp[n][last]
= solve(a, 2, n - 1);
}
else if (a[n - 1] == 2)
{
if (last == 1)
return dp[n][last] = 1
+ solve(a, 0, n - 1);
else
return dp[n][last]
= solve(a, 1, n - 1);
}
else
{
if (last == 1)
return dp[n][last]
= solve(a, 2, n - 1);
else if (last == 2)
return dp[n][last]
= solve(a, 1, n - 1);
else
return dp[n][last]
= min(solve(a, 2, n - 1),
solve(a, 1, n - 1));
}
}
let N = 4;
let arr = [ 0, 1, 3, 2 ];
for (let i=0;i<dp.length;i++)
{
dp[i] = new Array(3).fill(-1);
}
document.write(solve(arr, 0, N) + "<br>" )
</script>
|
Time Complexity: O(N), where N is the size of arr[].
Auxiliary Space: O(N), where N is the size of arr[].
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...