Skip to content
Related Articles

Related Articles

Improve Article

Minimum number of coins needed to remove all the elements of the array based on given rules

  • Difficulty Level : Medium
  • Last Updated : 15 Sep, 2021

Given an array arr of length N with values 1 and 2 indicating type 1 and type 2 elements and two players, player1 and player2. The task is to find the minimum number of coins required to remove all the elements in the order given in the array. Below rules must be followed:

  • Player1 and Player2 take turns removing elements with Player1 starting first
  • Both can remove at most 2 adjacent elements and must remove at least one element on their turn
  • Type 2 element can be removed by both of them without a coin
  • Type 1 element can be removed by Player2 without a coin but Player1 will need a coin to remove the element

Example:

Input: N = 8 arr = [1, 2, 1, 1, 2, 1, 1, 1]
Output: 2
Explanation: Total coins needed by Player1 is 2. Below are the elements removed at each player’s turn:

  • Player1 will remove the first element of type 1 using one coin and second element of type 2 without a coin
  • Player2 will remove the next two elements
  • Player1 will start its operation and remove only the next element of type 2 without a coin
  • Player2 will start its operation and remove next two elements at index 5 and 6 in the array
  • Player1 will remove the last element of type 1 using one coin

Input: N = 4 arr = [1, 1, 2, 2]
Output: 2
Explanation: Total coins needed by Player1 is 1.  Below are the elements removed at each player’s turn

  • Player1 will remove the first element of type 1 using one coin
  • Player2 will remove the 2nd and the 3rd element
  • Player1 will remove the 4th element of type 2 without a coin
 

Approach:  Given problem can be solved with greedy approach using two pointers. Below steps can be followed to solve the problem:



  • The first element is considered separately. If it is of type 1 then 1 will be added to the total coins need to remove the elements
  • Iterate the array from 2nd index considering its Player1’s turn
  • If on Player1’s turn and the first element is of type 2 and next element of type 1 then Player1 will remove only the element of type 2 and then Player2 can start the operation
  • If on Player1’s turn and two consecutive elements of type 2 then in that operation Player1 will remove both the elements
  • If on Player1’s turn and there are 3 consecutive elements of type 1 then Player1 will only remove the first element using one coin and the next two elements can be removed by Player2
  • Considering the above three cases an observation can be made that every chunk of type 1 elements start with Player2’s operation
  • Thus for every three consecutive elements of type 1, it can be seen that one coin is needed by Player1 to remove an element and two elements will be removed by Player2

C++




// C++ implementation for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum
// number of coins needed
int minimumcoins(int arr[], int N)
{
 
    int coins = 0;
    int j = 0;
 
    // Consider the first element
    // separately, add 1 to the total
    // if it's of type 1
    if (arr[0] == 1)
        coins++;
 
    // Iterate from the second element
    for (int i = 1; i < N; i++) {
        // If the current element is
        // of type 2 then any Player
        // can remove the element
        if (arr[i] == 2)
            continue;
 
        // Second pointer to reach end of
        // type 1 elements
        j = i;
 
        // Increment j until arr[j]
        // is equal to 1 and j is not
        // out of bounds
        while (j < N && arr[j] == 1) {
            j++;
        }
 
        // Number of type 1 elements
        // in a continious chunk
        int x = (j - i);
        coins += x / 3;
 
        // From next iteration i
        // pointer will start from
        // index of j
        i = j - 1;
    }
 
    // Return the minimum count of coins
    return coins;
}
 
int main()
 
{
 
    int N = 8;
 
    int arr[] = { 1, 2, 1, 1, 2, 1, 1, 1 };
 
    cout << minimumcoins(arr, N);
 
    return 0;
}

Java




// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to calculate minimum
    // number of coins needed
    static int minimumcoins(int arr[], int N)
    {
 
        int coins = 0;
        int j = 0;
 
        // Consider the first element
        // separately, add 1 to the total
        // if it's of type 1
        if (arr[0] == 1)
            coins++;
 
        // Iterate from the second element
        for (int i = 1; i < N; i++) {
            // If the current element is
            // of type 2 then any Player
            // can remove the element
            if (arr[i] == 2)
                continue;
 
            // Second pointer to reach end of
            // type 1 elements
            j = i;
 
            // Increment j until arr[j]
            // is equal to 1 and j is not
            // out of bounds
            while (j < N && arr[j] == 1) {
                j++;
            }
 
            // Number of type 1 elements
            // in a continious chunk
            int x = (j - i);
            coins += x / 3;
 
            // From next iteration i
            // pointer will start from
            // index of j
            i = j - 1;
        }
 
        // Return the minimum count of coins
        return coins;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int N = 8;
 
        int arr[] = { 1, 2, 1, 1, 2, 1, 1, 1 };
        // Function Call
        System.out.println(minimumcoins(arr, N));
    }
}
 
// This code is contributed by dwivediyash

C#




// C# implementation for the above approach
using System;
 
public class GFG {
 
    // Function to calculate minimum
    // number of coins needed
    static int minimumcoins(int []arr, int N)
    {
 
        int coins = 0;
        int j = 0;
 
        // Consider the first element
        // separately, add 1 to the total
        // if it's of type 1
        if (arr[0] == 1)
            coins++;
 
        // Iterate from the second element
        for (int i = 1; i < N; i++) {
            // If the current element is
            // of type 2 then any Player
            // can remove the element
            if (arr[i] == 2)
                continue;
 
            // Second pointer to reach end of
            // type 1 elements
            j = i;
 
            // Increment j until arr[j]
            // is equal to 1 and j is not
            // out of bounds
            while (j < N && arr[j] == 1) {
                j++;
            }
 
            // Number of type 1 elements
            // in a continious chunk
            int x = (j - i);
            coins += x / 3;
 
            // From next iteration i
            // pointer will start from
            // index of j
            i = j - 1;
        }
 
        // Return the minimum count of coins
        return coins;
    }
   
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 8;
 
        int []arr = { 1, 2, 1, 1, 2, 1, 1, 1 };
         
        // Function Call
        Console.WriteLine(minimumcoins(arr, N));
    }
}
 
// This code is contributed by AnkThon

Javascript




<script>
 
// JavaScript implementation for the above approach
 
// Function to calculate minimum
// number of coins needed
function minimumcoins(arr, N)
{
    let coins = 0;
    let j = 0;
     
    // Consider the first element
    // separately, add 1 to the total
    // if it's of type 1
    if (arr[0] == 1)
        coins++;
 
    // Iterate from the second element
    for(let i = 1; i < N; i++)
    {
         
        // If the current element is
        // of type 2 then any Player
        // can remove the element
        if (arr[i] == 2)
            continue;
 
        // Second pointer to reach end of
        // type 1 elements
        j = i;
 
        // Increment j until arr[j]
        // is equal to 1 and j is not
        // out of bounds
        while (j < N && arr[j] == 1)
        {
            j++;
        }
 
        // Number of type 1 elements
        // in a continious chunk
        let x = (j - i);
        coins += Math.floor(x / 3);
 
        // From next iteration i
        // pointer will start from
        // index of j
        i = j - 1;
    }
 
    // Return the minimum count of coins
    return coins;
}
 
// Driver code
let N = 8;
let arr = [ 1, 2, 1, 1, 2, 1, 1, 1 ];
 
document.write(minimumcoins(arr, N));
 
// This code is contributed by Potta Lokesh
 
</script>
Output
2

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :