Open In App

MEX Sum Count

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a length-N sequence A=(A1,A2 ,…,AN ) consisting of 0, 1, and 2, and a string S of size N consisting of M, E, and X. Find the sum of MEX(Ai, Aj, Ak) over all tuples of integers (i,j,k) such that 1≤i.

Example:

Input: sequenceLength = 4, sequenceA = {1, 1, 0, 2}, stringS = “MEEX”
Output: 3

Input: sequenceLength = 3, sequenceA = {0, 0, 0}, stringS = “XXX”
Output: 0

Approach:

The idea is to uses the concept of MEX (Minimum Excludant), which is the smallest non-negative integer that does not exist in the given set. Exhaustively try all j such that Sj = E and calculates the number of i’s with i<j, Si = M, and Ai = a, and the number of k’s with k>j, Sk = X, and Ai = b. Then multiplies these counts by mex(a,Aj,b) and adds the result to the answer. This is done for all i, j, and k, and the final answer is printed.

Steps:

  • Create two two-dimensional vectors, countM and countX, to keep track of the number of “M” and “X” characters before and after each position in the string S.
  • Iterate over the string S and for each character, if it’s ‘M’, increment the corresponding count in the countM array.
  • Similarly, iterate over the string S in reverse and for each character, if it’s ‘X’, increment the corresponding count in the countX array.
  • Initialize the answer to 0. Then, iterate over the string S and for each character, if it’s ‘E’, iterate over all possible values of sequence A (0, 1, 2) and for each pair of values, add to the answer the product of the corresponding counts in countM and countX and the MEX of the pair of values and the current value in sequence A.
  • Finally, print the calculated answer.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
 
// Function to calculate the MEX (Minimum Excludant) of
// three numbers
int calculateMEX(int num1, int num2, int num3)
{
    for (int i = 0; i < 3; i++) {
        if (num1 != i && num2 != i && num3 != i)
            return i;
    }
    return 3;
}
 
int main()
{
    // Static input
    int sequenceLength = 4;
    vector<int> sequenceA = { 1, 1, 0, 2 };
    string stringS = "MEEX";
 
    // Counters for the number of 'M' and 'X' characters
    // before and after each position
    vector<vector<int> > countM(sequenceLength + 1,
                                vector<int>(3));
    vector<vector<int> > countX(sequenceLength + 1,
                                vector<int>(3));
 
    // Calculate the countM array
    for (int i = 0; i < sequenceLength; i++) {
        countM[i + 1] = countM[i];
        if (stringS[i] == 'M')
            ++countM[i + 1][sequenceA[i]];
    }
 
    // Calculate the countX array
    for (int i = sequenceLength - 1; i >= 0; i--) {
        countX[i] = countX[i + 1];
        if (stringS[i] == 'X')
            ++countX[i][sequenceA[i]];
    }
 
    ll answer = 0;
    for (int i = 0; i < sequenceLength; i++) {
        if (stringS[i] != 'E')
            continue;
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                answer
                    += (ll)countM[i][j] * countX[i + 1][k]
                    * calculateMEX(j, sequenceA[i], k);
            }
        }
    }
    cout << answer << endl;
}


Java




import java.util.*;
public class MexCalculation {
 
    // Function to calculate the MEX (Minimum Excludant) of
    // three numbers
    static int calculateMEX(int num1, int num2, int num3)
    {
        for (int i = 0; i < 3; i++) {
            if (num1 != i && num2 != i && num3 != i)
                return i;
        }
        return 3;
    }
 
    public static void main(String[] args)
    {
        // Static input
        int sequenceLength = 4;
        List<Integer> sequenceA = Arrays.asList(1, 1, 0, 2);
        String stringS = "MEEX";
 
        // Counters for the number of 'M' and 'X' characters
        // before and after each position
        List<List<Integer> > countM
            = new ArrayList<>(Collections.nCopies(
                sequenceLength + 1,
                new ArrayList<>(Arrays.asList(0, 0, 0))));
        List<List<Integer> > countX
            = new ArrayList<>(Collections.nCopies(
                sequenceLength + 1,
                new ArrayList<>(Arrays.asList(0, 0, 0))));
 
        // Calculate the countM array
        for (int i = 0; i < sequenceLength; i++) {
            countM.set(i + 1,
                       new ArrayList<>(countM.get(i)));
            if (stringS.charAt(i) == 'M')
                countM.get(i + 1).set(
                    sequenceA.get(i),
                    countM.get(i + 1).get(sequenceA.get(i))
                        + 1);
        }
 
        // Calculate the countX array
        for (int i = sequenceLength - 1; i >= 0; i--) {
            countX.set(i,
                       new ArrayList<>(countX.get(i + 1)));
            if (stringS.charAt(i) == 'X')
                countX.get(i).set(
                    sequenceA.get(i),
                    countX.get(i).get(sequenceA.get(i))
                        + 1);
        }
 
        long answer = 0;
        for (int i = 0; i < sequenceLength; i++) {
            if (stringS.charAt(i) != 'E')
                continue;
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    answer += (long)countM.get(i).get(j)
                              * countX.get(i + 1).get(k)
                              * calculateMEX(
                                  j, sequenceA.get(i), k);
                }
            }
        }
        System.out.println(answer);
    }
}


Python3




# Python code to implement the approach
 
# Function to calculate the MEX
# (Minimum Excludant) of three numbers
 
 
def calculateMEX(num1, num2, num3):
    for i in range(3):
        if num1 != i and num2 != i and num3 != i:
            return i
    return 3
 
 
# Static input
sequenceLength = 4
sequenceA = [1, 1, 0, 2]
stringS = "MEEX"
 
# Counters for the number of 'M' and 'X'
# characters before and after each position
countM = [[0, 0, 0] for _ in range(sequenceLength + 1)]
countX = [[0, 0, 0] for _ in range(sequenceLength + 1)]
 
# Calculate the countM array
for i in range(sequenceLength):
    countM[i + 1] = countM[i][:]
    if stringS[i] == 'M':
        countM[i + 1][sequenceA[i]] += 1
 
# Calculate the countX array
for i in range(sequenceLength - 1, -1, -1):
    countX[i] = countX[i + 1][:]
    if stringS[i] == 'X':
        countX[i][sequenceA[i]] += 1
 
answer = 0
for i in range(sequenceLength):
    if stringS[i] != 'E':
        continue
    for j in range(3):
        for k in range(3):
            answer += countM[i][j] * countX[i + 1][k] * \
                calculateMEX(j, sequenceA[i], k)
 
print(answer)


C#




using System;
 
class Program
{
    // Function to calculate the MEX (Minimum Excludant) of three numbers
    static int CalculateMEX(int num1, int num2, int num3)
    {
        for (int i = 0; i < 3; i++)
        {
            if (num1 != i && num2 != i && num3 != i)
                return i;
        }
        return 3;
    }
 
    static void Main()
    {
        // Static input
        int sequenceLength = 4;
        int[] sequenceA = { 1, 1, 0, 2 };
        string stringS = "MEEX";
 
        // Counters for the number of 'M' and 'X' characters before and after each position
        int[][] countM = new int[sequenceLength + 1][];
        int[][] countX = new int[sequenceLength + 1][];
 
        for (int i = 0; i <= sequenceLength; i++)
        {
            countM[i] = new int[3];
            countX[i] = new int[3];
        }
 
        // Calculate the countM array
        for (int i = 0; i < sequenceLength; i++)
        {
            countM[i + 1] = (int[])countM[i].Clone();
            if (stringS[i] == 'M')
                countM[i + 1][sequenceA[i]] += 1;
        }
 
        // Calculate the countX array
        for (int i = sequenceLength - 1; i >= 0; i--)
        {
            countX[i] = (int[])countX[i + 1].Clone();
            if (stringS[i] == 'X')
                countX[i][sequenceA[i]] += 1;
        }
 
        long answer = 0;
        for (int i = 0; i < sequenceLength; i++)
        {
            if (stringS[i] != 'E')
                continue;
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    answer += countM[i][j] * countX[i + 1][k] * CalculateMEX(j, sequenceA[i], k);
                }
            }
        }
 
        Console.WriteLine(answer);
    }
}


Javascript




// Function to calculate the MEX (Minimum Excludant) of three numbers
function calculateMEX(num1, num2, num3) {
    for (let i = 0; i < 3; i++) {
        if (num1 !== i && num2 !== i && num3 !== i)
            return i;
    }
    return 3;
}
 
// Main function
function main() {
    // Static input
    const sequenceLength = 4;
    const sequenceA = [1, 1, 0, 2];
    const stringS = "MEEX";
 
    // Counters for the number of 'M' and 'X' characters
    // before and after each position
    const countM = Array.from({ length: sequenceLength + 1 }, () => Array(3).fill(0));
    const countX = Array.from({ length: sequenceLength + 1 }, () => Array(3).fill(0));
 
    // Calculate the countM array
    for (let i = 0; i < sequenceLength; i++) {
        countM[i + 1] = [...countM[i]];
        if (stringS[i] === 'M')
            ++countM[i + 1][sequenceA[i]];
    }
 
    // Calculate the countX array
    for (let i = sequenceLength - 1; i >= 0; i--) {
        countX[i] = [...countX[i + 1]];
        if (stringS[i] === 'X')
            ++countX[i][sequenceA[i]];
    }
 
    let answer = 0;
    for (let i = 0; i < sequenceLength; i++) {
        if (stringS[i] !== 'E')
            continue;
        for (let j = 0; j < 3; j++) {
            for (let k = 0; k < 3; k++) {
                answer += countM[i][j] * countX[i + 1][k] * calculateMEX(j, sequenceA[i], k);
            }
        }
    }
    console.log(answer);
}
 
// Call the main function
main();


Output

3

Time complexity: O(N), where N is the length of the sequence A and the string S. This is because the solution performs a single pass over the sequence A and the string S to calculate the countM and countX arrays, and then another pass to calculate the answer.

Auxiliary space: O(N), where N is the length of the sequence A and the string S. This is because the solution uses two two-dimensional vectors, countM and countX, to store the counts of ‘M’ and ‘X’



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads