Open In App

Minimum number of days in which no task is performed

Improve
Improve
Like Article
Like
Save
Share
Report

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)
{
    // Base case
    if (n == 0)
        return 0;
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return 1 + solve(a, 0, n - 1);
    }
 
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task B
            return solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A
        // so can't perform it again
        if (last == 1)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return solve(a, 1, n - 1);
    }
 
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
 
            // Perform task A
            return solve(a, 1, n - 1);
        else
 
            // Perform the minimum among both
            return min(solve(a, 2, n - 1),
                       solve(a, 1, n - 1));
    }
}
 
int main()
{
    // Number of days
    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)
{
    // Base case
    if (n == 0)
        return 0;
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return 1 + solve(a, 0, n - 1);
    }
 
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task B
            return solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A
        // so can't perform it again
        if (last == 1)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return solve(a, 1, n - 1);
    }
 
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
 
            // Perform task A
            return solve(a, 1, n - 1);
        else
 
            // Perform the minimum among both
            return Math.min(solve(a, 2, n - 1),
                       solve(a, 1, n - 1));
    }
}
 
public static void main(String args[])
{
    // Number of days
    int N = 4;
    int []arr = { 0, 1, 3, 2 };
 
    System.out.println(solve(arr, 0, N));
 
}
}
// This code is contributed by Samim Hossain Mondal.


Python3




def solve(a, last, n):
   
    # Base case
    if (n == 0):
        return 0
 
    # Condition 1 (no task) so does nothing
    if (a[n - 1] == 0):
       
        # Increase count
        return 1 + solve(a, 0, n - 1)
     
 
    # Condition 2 (only task B)
    elif (a[n - 1] == 1):
       
        # Last task is of type B
        # so can't perform it again
        if (last == 2):
           
            # Increase count
            return 1 + solve(a, 0, n - 1)
        else:
            # Perform task B
            return solve(a, 2, n - 1)
     
 
    # Condition 3 (only task A )
    elif (a[n - 1] == 2):
       
        # Last task is of type A
        # so can't perform it again
        if (last == 1):
           
            # Increase count
            return 1 + solve(a, 0, n - 1)
        else:
            # Perform task A
            return solve(a, 1, n - 1)
     
 
    # Condition 4 (both task A and B)
    else:
        # Last task is of type A
        if (last == 1):
 
            # Perform task B
            return solve(a, 2, n - 1)
 
        # Last task is of type B
        elif (last == 2):
 
            # Perform task A
            return solve(a, 1, n - 1)
        else:
 
            # Perform the minimum among both
            return min(solve(a, 2, n - 1),
                        solve(a, 1, n - 1))
     
# Number of days
N = 4
arr = [0, 1, 3, 2]
 
print(solve(arr, 0, N))
 
# This code is contributed by gfgking.


C#




using System;
 
class GFG
{
 
static int solve(int []a, int last, int n)
{
    // Base case
    if (n == 0)
        return 0;
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return 1 + solve(a, 0, n - 1);
    }
 
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task B
            return solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A
        // so can't perform it again
        if (last == 1)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return solve(a, 1, n - 1);
    }
 
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
 
            // Perform task A
            return solve(a, 1, n - 1);
        else
 
            // Perform the minimum among both
            return Math.Min(solve(a, 2, n - 1),
                       solve(a, 1, n - 1));
    }
}
 
public static void Main()
{
    // Number of days
    int N = 4;
    int []arr = { 0, 1, 3, 2 };
 
    Console.Write(solve(arr, 0, N));
 
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
 
function solve(a, last, n)
{
    // Base case
    if (n == 0)
        return 0;
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return 1 + solve(a, 0, n - 1);
    }
 
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task B
            return solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A
        // so can't perform it again
        if (last == 1)
 
            // Increase count
            return 1 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return solve(a, 1, n - 1);
    }
 
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
 
            // Perform task A
            return solve(a, 1, n - 1);
        else
 
            // Perform the minimum among both
            return Math.min(solve(a, 2, n - 1),
                       solve(a, 1, n - 1));
    }
}
 
// Number of days
let N = 4;
let arr = [ 0, 1, 3, 2 ];
 
document.write(solve(arr, 0, N));
 
// This code is contributed by Samim Hossain Mondal.
</script>


Time Complexity: O(2n)
Auxiliary Space: O(1)

Output

2

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++




// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
int dp[101][3];
 
int solve(int a[], int last, int n)
{
    // Base case
    if (n == 0)
        return 0;
 
    // If the value is pre-calculated return it
    if (dp[n][last] != -1)
        return dp[n][last];
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return dp[n][last] = 1
                             + solve(a, 0, n - 1);
    }
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A so can't
 
        if (last == 1)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return dp[n][last]
                   = solve(a, 1, n - 1);
    }
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
            return dp[n][last]
                   = solve(a, 1, n - 1);
 
        // Perform task A
        else
 
            // Perform the minimum among both
            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 };
 
    // Initialize the space with -1
    memset(dp, -1, sizeof(dp));
 
    cout << solve(arr, 0, N) << endl;
 
    return 0;
}


Java




// Java Program to implement
// the above approach
import java.util.*;
 
public class GFG
{
     
static int dp[][] = new int[101][3];
 
static int solve(int []a, int last, int n)
{
    // Base case
    if (n == 0)
        return 0;
 
    // If the value is pre-calculated return it
    if (dp[n][last] != -1)
        return dp[n][last];
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return dp[n][last] = 1
                             + solve(a, 0, n - 1);
    }
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A so can't
 
        if (last == 1)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return dp[n][last]
                   = solve(a, 1, n - 1);
    }
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
            return dp[n][last]
                   = solve(a, 1, n - 1);
 
        // Perform task A
        else
 
            // Perform the minimum among both
            return dp[n][last]
                   = Math.min(solve(a, 2, n - 1),
                         solve(a, 1, n - 1));
    }
}
 
public static void main(String args[])
{
    // Number of days
    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));
 
}
}
// This code is contributed by Samim Hossain Mondal.


Python3




# Python3 program to implement
# the above approach
dp = [0] * 101
 
def solve(a, last, n):
     
    # Base case
    if (n == 0):
        return 0
 
    # If the value is pre-calculated return it
    if (dp[n][last] != -1):
        return dp[n][last]
 
    # Condition 1 (no task) so does nothing
    if (a[n - 1] == 0):
         
        # Increase count
        dp[n][last] = 1 + solve(a, 0, n - 1)
 
        return dp[n][last]
 
    # Condition 2 (only task B)
    elif (a[n - 1] == 1):
 
        # Last task is of type B
        # so can't perform it again
        if (last == 2):
             
            # Increase count
            dp[n][last] = 1 + solve(a, 0, n - 1)
            return dp[n][last]
        else:
             
            # Perform task B
            dp[n][last] = solve(a, 2, n - 1)
            return dp[n][last]
 
    # Condition 3 (only task A )
    elif (a[n - 1] == 2):
 
        # Last task is of type A so can't
        if (last == 1):
 
            # Increase count
            dp[n][last] = 1 + solve(a, 0, n - 1)
            return dp[n][last]
        else:
            dp[n][last] = solve(a, 1, n - 1)
             
            # Perform task A
            return dp[n][last]
 
    # Condition 4 (both task A and B)
    else:
 
        # Last task is of type A
        if (last == 1):
            dp[n][last] = solve(a, 2, n - 1)
             
            # Perform task B
            return dp[n][last]
 
        # Last task is of type B
        elif (last == 2):
            dp[n][last] = solve(a, 1, n - 1)
            return dp[n][last]
 
        # Perform task A
        else:
            dp[n][last] = min(solve(a, 2, n - 1),
                              solve(a, 1, n - 1))
             
            # Perform the minimum among both
            return dp[n][last]
 
# Driver code
N = 4
arr = [ 0, 1, 3, 2 ]
 
# Initialize the space with -1
for i in range(len(dp)):
    dp[i] = [-1] * 3
 
print(solve(arr, 0, N))
 
# This code is contributed by Saurabh Jaiswal


C#




// C# Program to implement
// the above approach
using System;
 
class GFG
{
     
static int [,]dp = new int[101, 3];
 
static int solve(int []a, int last, int n)
{
    // Base case
    if (n == 0)
        return 0;
 
    // If the value is pre-calculated return it
    if (dp[n, last] != -1)
        return dp[n, last];
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return dp[n, last] = 1
                             + solve(a, 0, n - 1);
    }
    // Condition 2 (only task B)
    else if (a[n - 1] == 1) {
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return dp[n, last] = 1
                                 + solve(a, 0, n - 1);
        else
            // Perform task B
            return dp[n, last]
                   = solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2) {
        // Last task is of type A so can't
 
        if (last == 1)
 
            // Increase count
            return dp[n, last] = 1
                                 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return dp[n, last]
                   = solve(a, 1, n - 1);
    }
    // Condition 4 (both task A and B)
    else {
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return dp[n, last]
                   = solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
            return dp[n, last]
                   = solve(a, 1, n - 1);
 
        // Perform task A
        else
 
            // Perform the minimum among both
            return dp[n, last]
                   = Math.Min(solve(a, 2, n - 1),
                         solve(a, 1, n - 1));
    }
}
 
public static void Main()
{
    // Number of days
    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));
 
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript




  <script>
 
// JavaScript Program to implement
// the above approach
 
let  dp = new Array(101)
function solve(a, last, n)
{
 
    // Base case
    if (n == 0)
        return 0;
 
    // If the value is pre-calculated return it
    if (dp[n][last] != -1)
        return dp[n][last];
 
    // Condition 1 (no task) so does nothing
    if (a[n - 1] == 0) {
        // Increase count
        return dp[n][last] = 1
                             + solve(a, 0, n - 1);
    }
     
    // Condition 2 (only task B)
    else if (a[n - 1] == 1)
    {
     
        // Last task is of type B
        // so can't perform it again
        if (last == 2)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
    }
 
    // Condition 3 (only task A )
    else if (a[n - 1] == 2)
    {
     
        // Last task is of type A so can't
        if (last == 1)
 
            // Increase count
            return dp[n][last] = 1
                                 + solve(a, 0, n - 1);
        else
 
            // Perform task A
            return dp[n][last]
                   = solve(a, 1, n - 1);
    }
     
    // Condition 4 (both task A and B)
    else
    {
     
        // Last task is of type A
        if (last == 1)
 
            // Perform task B
            return dp[n][last]
                   = solve(a, 2, n - 1);
 
        // Last task is of type B
        else if (last == 2)
            return dp[n][last]
                   = solve(a, 1, n - 1);
 
        // Perform task A
        else
 
            // Perform the minimum among both
            return dp[n][last]
                   = min(solve(a, 2, n - 1),
                         solve(a, 1, n - 1));
    }
}
 
    let N = 4;
    let arr = [ 0, 1, 3, 2 ];
 
    // Initialize the space with -1
    for(let i=0;i<dp.length;i++)
    {
        dp[i] = new Array(3).fill(-1);
    }
 
   document.write(solve(arr, 0, N) + "<br>")
 
 
    // This code is contributed by Potta Lokesh
    </script>


Output

2

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
Previous
Next
Share your thoughts in the comments
Similar Reads