Tuesday, 19 September 2017

CodeForces Problem 862A - Mahmoud and Ehab and the MEX Solution (C++)

Problem:

Please find the problem here.


Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Explanation:
1.MEX has two condition
2.MEX equal to entered input ... MEX will increase by 1 as element is erased.
3.MEX less than input...MEX will decline as element is inserted before MEX
4.Goodluck!

Code:
#include<bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
int n;
int k;

int x;
cin>>n>>k;
int ans=k;
FOR(i,n){
cin>>x;
if(x<k){
ans--;
}
else if(x==k){
ans++;
}
}
cout<<ans;
}

1 comment:

  1. sir tell me more about this problem.....am new student plz sir .......i don't understand this problem.

    ReplyDelete

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...