Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Maps

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

The mainly used member functions of maps are:

  • Map Template:
    map <key_type, data_type>
  • Declaration
    map<string,int> m; //Creates a map m where key_type is of type string and data_type is of type int.
  • Size:
    int length = m.size(); //Gives the size of the map.
  • Insert:
    m.insert(make_pair("hello",9)); //Here the pair is inserted into the map where the key is "hello" and the value associated with it is 9.
  • Erasing an element:
    m.erase(val); //Erases the pair from the map where the key_type is val.
  • Finding an element:
    map<string,int>::iterator itr=m.find(val); //Gives the iterator to the element val if it is found otherwise returns m.end() .
    Ex: map<string,int>::iterator itr=m.find("Maps"); //If Maps is not present as the key value then itr==m.end().
  • Accessing the value stored in the key:
    To get the value stored of the key "MAPS" we can do m["MAPS"] or
    we can get the iterator using the find function and then by itr->second we can access the value.

Try the following example in the editor below.

There are some boxes and each having a unique id. You are given Q queries of three types to be performed on the boxes.

  • 1 X Y : Add the Y balls to the box whose id is X.
  • 2 X : Remove all balls from the box whose id is X.
  • 3 X : Print the number of balls in the box id X.

Input Format

  • The first line of the input contains Q where Q is the number of queries.
  • The next Q lines contain 1 query each. The first integer, type of each query is the type of the query.
    • If query is of type 1, it consists of two integer X and Y.
    • If query is of type 2 or 3, it consists of a single integer X where X is the id of box.

Constraints

1 <= Q <= 105 
1 <= Y <= 104
1 <= X <= 109

Sample Input

7
1 3 5
1 2 1
2 4
3 3
2 3
3 3
3 2

Sample Output

5
0
1
Start solving Maps on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test