We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Stacks
  4. Game of Two Stacks

Game of Two Stacks

Problem
Submissions
Leaderboard
Discussions
Editorial

Alexa has two stacks of non-negative integers, stack and stack where index denotes the top of the stack. Alexa challenges Nick to play the following game:

  • In each move, Nick can remove one integer from the top of either stack or stack .
  • Nick keeps a running sum of the integers he removes from the two stacks.
  • Nick is disqualified from the game if, at any point, his running sum becomes greater than some integer given at the beginning of the game.
  • Nick's final score is the total number of integers he has removed from the two stacks.

Given , , and for games, find the maximum possible score Nick can achieve.

Example

The maximum number of values Nick can remove is . There are two sets of choices with this result.

  1. Remove from with a sum of .
  2. Remove from and from with a sum of .

Function Description
Complete the twoStacks function in the editor below.

twoStacks has the following parameters: - int maxSum: the maximum allowed sum
- int a[n]: the first stack
- int b[m]: the second stack

Returns
- int: the maximum number of selections Nick can make

Input Format

The first line contains an integer, (the number of games). The subsequent lines describe each game in the following format:

  1. The first line contains three space-separated integers describing the respective values of (the number of integers in stack ), (the number of integers in stack ), and (the number that the sum of the integers removed from the two stacks cannot exceed).
  2. The second line contains space-separated integers, the respective values of .
  3. The third line contains space-separated integers, the respective values of .

Constraints

Subtasks

  • for of the maximum score.

Sample Input 0

1
5 4 10
4 2 4 6 1
2 1 8 5

Sample Output 0

4

Explanation 0

The two stacks initially look like this:

image

The image below depicts the integers Nick should choose to remove from the stacks. We print as our answer, because that is the maximum number of integers that can be removed from the two stacks without the sum exceeding .

image

(There can be multiple ways to remove the integers from the stack, the image shows just one of them.)

Author

Shafaet

Difficulty

Medium

Max Score

30

Submitted By

46679

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy