Help me in solving CARDSWIPE problem

My issue

In the test case 1
First swipe 1 enter into office
Second swipe 2 enter into office
Third swipe 2 left office
Forth swipe 1 left office
So,no one in office but output is 2
In your theory
But remaining are same as theory

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	}
}

Problem Link: CARDSWIPE Problem - CodeChef

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    vector<int> v(n);
	    for(int i=0;i<n;i++){
	        cin>>v[i];
	    }
	    
	    unordered_map<int,int> m;
	    int maxi = 0;
	    int curr = 0;
	    for(int i=0;i<n;i++){
	        if(m[v[i]]){
	            //already in the map
	            curr--;
	            m[v[i]] = 0;
	        }else{
	            curr++;
	            m[v[i]] = 1;
	        }
	        maxi = max(curr,maxi);
	    }
	    cout<<maxi<<endl;
	}
	return 0;
}

you can solve it like this

1 Like