MEX Sum Count
Last Updated :
29 Jan, 2024
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 ;
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()
{
int sequenceLength = 4;
vector< int > sequenceA = { 1, 1, 0, 2 };
string stringS = "MEEX" ;
vector<vector< int > > countM(sequenceLength + 1,
vector< int >(3));
vector<vector< int > > countX(sequenceLength + 1,
vector< int >(3));
for ( int i = 0; i < sequenceLength; i++) {
countM[i + 1] = countM[i];
if (stringS[i] == 'M' )
++countM[i + 1][sequenceA[i]];
}
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 {
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)
{
int sequenceLength = 4 ;
List<Integer> sequenceA = Arrays.asList( 1 , 1 , 0 , 2 );
String stringS = "MEEX" ;
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 ))));
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 );
}
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
def calculateMEX(num1, num2, num3):
for i in range ( 3 ):
if num1 ! = i and num2 ! = i and num3 ! = i:
return i
return 3
sequenceLength = 4
sequenceA = [ 1 , 1 , 0 , 2 ]
stringS = "MEEX"
countM = [[ 0 , 0 , 0 ] for _ in range (sequenceLength + 1 )]
countX = [[ 0 , 0 , 0 ] for _ in range (sequenceLength + 1 )]
for i in range (sequenceLength):
countM[i + 1 ] = countM[i][:]
if stringS[i] = = 'M' :
countM[i + 1 ][sequenceA[i]] + = 1
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
{
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()
{
int sequenceLength = 4;
int [] sequenceA = { 1, 1, 0, 2 };
string stringS = "MEEX" ;
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];
}
for ( int i = 0; i < sequenceLength; i++)
{
countM[i + 1] = ( int [])countM[i].Clone();
if (stringS[i] == 'M' )
countM[i + 1][sequenceA[i]] += 1;
}
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 calculateMEX(num1, num2, num3) {
for (let i = 0; i < 3; i++) {
if (num1 !== i && num2 !== i && num3 !== i)
return i;
}
return 3;
}
function main() {
const sequenceLength = 4;
const sequenceA = [1, 1, 0, 2];
const stringS = "MEEX" ;
const countM = Array.from({ length: sequenceLength + 1 }, () => Array(3).fill(0));
const countX = Array.from({ length: sequenceLength + 1 }, () => Array(3).fill(0));
for (let i = 0; i < sequenceLength; i++) {
countM[i + 1] = [...countM[i]];
if (stringS[i] === 'M' )
++countM[i + 1][sequenceA[i]];
}
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);
}
main();
|
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’
Share your thoughts in the comments
Please Login to comment...