Open In App

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

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:

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:




// 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 continuous 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 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 continuous 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




# Python program for the above approach
 
# Function to calculate minimum
# number of coins needed
def minimumcoins(arr, N) :
 
    coins = 0
    j = 0
 
    # Consider the first element
    # separately, add 1 to the total
    # if it's of type 1
    if (arr[0] == 1) :
        coins += 1
 
    # Iterate from the second element
    for i in range(1, N) :
        # 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 and arr[j] == 1) :
            j += 1
         
 
        # Number of type 1 elements
        # in a continuous chunk
        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
N = 8
arr = [ 1, 2, 1, 1, 2, 1, 1, 1 ]
 
print(minimumcoins(arr, N))
 
# This code is contributed by sanjoy_62.




// 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 continuous 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




<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 continuous 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)


Article Tags :