EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2023
  3. Preliminary Round

Codey and Money

https://www.hackerrank.com/contests/codenection-2023-preliminary-round-closed-category/challenges/cn-c5

Question

Codey is planning a trip to Penang, and it needs to gather exactly n ringgits for its exciting journey. However, it only has access to a limited number of ringgit bills, each of which is a power of 101010. The ringgit bills that Codey has been defined by an array a with a length of k, where represents the number of 10i−110^{i-1}10i−1 ringgit bills that Codey possesses.

For example, when k=3,a=[3,2,1]k = 3, a = [3, 2, 1]k=3,a=[3,2,1], Codey has three 111 ringgit bills, two 101010 ringgit bills, and one 100100100 ringgit bill.

Codey would like you to determine if it's possible to obtain precisely n ringgits for its trip using the ringgit bills it has.

Input Format

The first line contains an integer t, which represents the number of test cases.

The following provides the description of each test case:

  • The first line contains an integer k, which represents the length of array a.

  • The second line contains an integer n, which represents the final amount Codey needs.

  • The third line contains k integers a1,a2,...,aka_1, a_2, ..., a_ka1​,a2​,...,ak​, each representing the number of 10i−110^{i-1}10i−1 ringgit bills.

Constraints

1≤t≤5001 \le t \le 5001≤t≤500
1≤k≤1051 \le k \le 10^51≤k≤105
1≤n≤10k1 \le n \le 10^k1≤n≤10k
0≤ai≤1050 \le a_i \le 10^50≤ai​≤105
  • It is guaranteed that the sum of k over all test cases does not exceed 5 * 10^5.

Output Format

Outputs YES for test case if it's possible to obtain n ringgits, otherwise output NO.

Sample Inputs:

Input

1
4
357
7 6 4 11

Output

YES

Explanation

The final amount n can be formed using 7 bills of 1 ringgit, 5 bills of 10 ringgit and 3 bills of 100 ringgit.

Input

2
2
11
2 3
2
1
0 1

Output

YES
NO

Explanation

In second test case, even though you have a 10 ringgit bill, you cannot split the bill to form exactly 1 ringgit.


Solution - So many constraints QAQ

I had tried a lot of method to solve this question, but only this one work and passed all the test cases.

Basically, there are 3 things need to consider:

  • The larger money can't split into smaller money

  • The smaller money can merge into larger money

  • Given largest currency amount is < 10, If the required money digit length is bigger than the largest currency, that is 100% no.

My personal logic would be:

  1. Split the required amount of money into arrays and reverse it.

  2. Check if the money[i] is smaller than the required money digits[i], if yes, that is instant NO.

  3. Otherwise, subtract that, and divide the current money[i] by 10, then add onto the next money[i], and so on.

In illustrate, it would look like this:

Requires: 351
Money: 11 7 6 4

Step 1: Split and Reverse the required money
Requires: [1, 5, 3]
Money: [11, 7, 6, 4]

Step 2: Compare Requires[0] < Money[0]
Requires: [5, 3]
Money: [10, 7, 6, 4]

Step 3: Money[0] // 10, then add onto Money[1]
Requires: [5, 3]
Money: [8, 6, 4]

...

Step 6:
Requires: []
Money: [3, 4]

Since the Requires is empty, the answer is YES.

Here's the full code for the implementation:

def compare_array_and_digits(array, digits):
    digits.reverse()

    for i in range(len(digits)):
        if array[i] < digits[i]:
            return "NO"
        else:
            array[i] -= digits[i]
            try:
                array[i+1] += array[i] // 10
            except:
                pass
    return "YES"

t = int(input().strip())
for a0 in range(t):
    n = int(input().strip())
    req = [int(digit) for digit in str(input())]
    have = list(map(int, input().strip().split()))
    
    print(compare_array_and_digits(have, req))

Note that I used try, except, pass to override the index out of range error. This technic is useful when it comes to competitive programming, when you simply don't want to handle the array leakage.

PreviousCodey and TextbooksNextCodey and Team Selection

Last updated 2 months ago

🇲🇾