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
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# Coin Queue

Problem Description

You need to maintain a queue that is initially empty. You will perform three types of operations on the queue -

1 X - Add the number x to the queue.

2 0 - Remove the least recently added number from the queue.

3 X - Consider the current numbers in the queue. Find the maximum size of a subset of numbers in the queue whose sum is X. If there is no such subset, return -1.

A subset is defined as a sequence that can be obtained by removing some (possibly all) elements present in the queue.

Problem Constraints

1 <= X <= 350

|A| <= 105

Input Format
The only argument contains a 2D array A, denoting the operations on the queue.

Output Format
Return an array containing the answers to each query of type 3.

Example Input

Input 1:

```  A :
[
[1, 3]
[1, 1]
[1, 1]
[2, 0]
[3, 2]
]
```

Input 2:

```  A :
[
[1, 1]
[1, 2]
[1, 2]
[1, 3]
[3, 4]
[3, 5]
]
```

Example Output

Output 1:

```  [2]
```

Output 2:

```  [2, 3]
```

Example Explanation

Explanation 1:

```  After first 3 operations, queue becomes {3, 1, 1}.
Then we pop the number 3. The queue is {1, 1}.
Then we can form 2 by taking both the ones. So, we have a subset of 2 numbers.
```

Explanation 2:

```  After first 3 operations, queue becomes {1, 2, 2, 3}.
Then 4 can be formed by 2 + 2 and 1 + 3. Max 2 numbers are in a subset.
5 can be formed by 1 + 2 + 2, 2 + 3 and 2 + 3. Max 3 numbers are in a subset.
```

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a question? Checkout Sample Codes for more details.
Hints
• Hint 1
• Solution Approach
• Complete Solution

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
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
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer