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++
#include <bits/stdc++.h>
using namespace std;
int minimumcoins( int arr[], int N)
{
int coins = 0;
int j = 0;
if (arr[0] == 1)
coins++;
for ( int i = 1; i < N; i++) {
if (arr[i] == 2)
continue ;
j = i;
while (j < N && arr[j] == 1) {
j++;
}
int x = (j - i);
coins += x / 3;
i = j - 1;
}
return coins;
}
int main()
{
int N = 8;
int arr[] = { 1, 2, 1, 1, 2, 1, 1, 1 };
cout << minimumcoins(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int minimumcoins( int arr[], int N)
{
int coins = 0 ;
int j = 0 ;
if (arr[ 0 ] == 1 )
coins++;
for ( int i = 1 ; i < N; i++) {
if (arr[i] == 2 )
continue ;
j = i;
while (j < N && arr[j] == 1 ) {
j++;
}
int x = (j - i);
coins += x / 3 ;
i = j - 1 ;
}
return coins;
}
public static void main(String[] args)
{
int N = 8 ;
int arr[] = { 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 };
System.out.println(minimumcoins(arr, N));
}
}
|
Python3
def minimumcoins(arr, N) :
coins = 0
j = 0
if (arr[ 0 ] = = 1 ) :
coins + = 1
for i in range ( 1 , N) :
if (arr[i] = = 2 ) :
continue
j = i
while (j < N and arr[j] = = 1 ) :
j + = 1
x = (j - i)
coins + = x / / 3
i = j - 1
return coins
N = 8
arr = [ 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 ]
print (minimumcoins(arr, N))
|
C#
using System;
public class GFG {
static int minimumcoins( int []arr, int N)
{
int coins = 0;
int j = 0;
if (arr[0] == 1)
coins++;
for ( int i = 1; i < N; i++) {
if (arr[i] == 2)
continue ;
j = i;
while (j < N && arr[j] == 1) {
j++;
}
int x = (j - i);
coins += x / 3;
i = j - 1;
}
return coins;
}
public static void Main(String[] args)
{
int N = 8;
int []arr = { 1, 2, 1, 1, 2, 1, 1, 1 };
Console.WriteLine(minimumcoins(arr, N));
}
}
|
Javascript
<script>
function minimumcoins(arr, N)
{
let coins = 0;
let j = 0;
if (arr[0] == 1)
coins++;
for (let i = 1; i < N; i++)
{
if (arr[i] == 2)
continue ;
j = i;
while (j < N && arr[j] == 1)
{
j++;
}
let x = (j - i);
coins += Math.floor(x / 3);
i = j - 1;
}
return coins;
}
let N = 8;
let arr = [ 1, 2, 1, 1, 2, 1, 1, 1 ];
document.write(minimumcoins(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Dec, 2021
Like Article
Save Article