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. Final Round

Codey and Apples

https://www.hackerrank.com/contests/codenection-2023-final-round-open-category/challenges/cn-c23

PreviousCodey and SightseeingNextCodey and Facto

Last updated 2 months ago

Question

An apple a day keeps the doctor away. Codey is a good friend and would like to keep its friends away from the doctors and keep them healthy.

Codey has n bags, where the iii-th bag contains aia_iai​ apples, and it has decided to share its apples with its m friends. In order to keep this fair, Codey will only share the apples with its friends if it can choose some bags (at least 1 and possibly all) and evenly divide all the apples in those bags among the m friends. In other words, the total number of apples in the bags Codey has chosen must be divisible equally among the m friends.

Your task is to find the bags that meet this condition. If there are multiple answers, you may print any of them. If it is not possible, output −1-1−1.

Note that this problem uses a custom checker, so make sure your program compiles correctly and prints the output according to the format before submitting.

Input Format

The first line contains integers n, m, which represent the number of bags and the number of friends, respectively.

The second line of input contains n integers a1,a2,...,ana_1, a_2, ... , a_na1​,a2​,...,an​, each representing the number of apples in the iii-th bag.

Constraints

1≤m≤n≤1051 \le m \le n \le 10^51≤m≤n≤105
1≤ai≤1051 \le a_i \le 10^51≤ai​≤105

Output Format

In the next line, output k unique integers separated by space, which are the indices of the selected bags for the second line.

Sample Inputs:

Input

5 4
1 2 3 4 5

Output

1
4

Explanation

Codey can choose the fourth bag and get 4 apples. Codey can then split one apple to each friend.

Input

5 4
1 1 1 1 1

Output

4
1 2 3 4

Explanation


Current - 16/18 test cases

This solution is still not 100%, it still got 2 test cases wrong Haven't figure it out why that particular 2 test case is wrong. But here's my current progress:

def distribute_apples(n, m, apples):
    indexed_apples = [(apples[i], i) for i in range(n)]

    indexed_apples.sort(reverse=True, key=lambda x: x[0])

    total_apples_distributed = 0
    bags_used = 0
    bags_list = []

    for i in range(n):
        total_apples_distributed += indexed_apples[i][0]
        bags_list.append(indexed_apples[i][1])
        bags_used += 1

        if total_apples_distributed % m == 0:
            return bags_used, bags_list

    return -1, None

n, m = map(int, input().strip().split())
apples = list(map(int, input().strip().split()))

result, used_bags = distribute_apples(n, m, apples)

print(result)
if used_bags is not None:
    print(" ".join(map(str, [bag + 1 for bag in used_bags]))) 
Solution 1 - Mapping

This one is solved by one of my friends, he used a mod_map method to keep track of the remainders.

If 2 set of values gives the same remainder, it means the both numbers can be combined together as a subset.

if the subsets remainder hits 0, then that is the solution. Otherwise, if all subsets is traversed, we still can't find no remainder cases, it will print -1 instead.

Here's my friend's solution:

def distribute_apples(n, m, apples):
    mod_map = {0: -1}
    prefix_sum = 0

    for i in range(n):
        prefix_sum += apples[i]
        remainder = prefix_sum % m

        if remainder in mod_map:
            start_index = mod_map[remainder] + 1
            end_index = i
            bags_used = end_index - start_index + 1
            bags_list = list(range(start_index, end_index + 1))
            return bags_used, bags_list

        mod_map[remainder] = i

    return -1, None

n, m = map(int, input().strip().split())
apples = list(map(int, input().strip().split()))

result, used_bags = distribute_apples(n, m, apples)

print(result)
if used_bags is not None:
    print(" ".join(map(str, [bag + 1 for bag in used_bags]))) 

Output the number of bags chosen, k∈{k∣1≤k≤n}k \in \{k | 1 \le k \le n\}k∈{k∣1≤k≤n} in the first line if it meets the condition.

If there are multiple answers, you may print any of them. If it is not possible, output −1-1−1.

Codey can choose the first 4 bags and get 1+1+1+1=41+1+1+1 = 41+1+1+1=4 apples. Codey can then split one apple to each friend.

🇲🇾