Google Kickstart with Go

Alec Garza
Geek Culture
Published in
3 min readFeb 16, 2022

--

Google Kickstart 2022 Practice Round Problem 1 Solution in Golang

src: Google Kick Start — Important Dates, Eligibility, Contest Details — GeeksforGeeks

Introduction

Over the past few months, I have been learning Go while prepping for technical interviews and competitive coding competitions. To balance these tasks, I have been doing my interview/competition prep in Golang.

Google Kickstart 2022 has begun, with the practice round kicking off this past week. Python, Java, C/C++, and C# are by far the most popular languages used in coding competitions. There is little to no online support for Leetcode, algorithms, or competitive coding in Golang. Go is one of the most desired coding languages to learn, but it is still fairly modern and gaining adoption. Due to this, the lack of online support is understandable and will surely grow.

I write articles about solving algorithm problems in Go for those that are curious, and this article will begin a series of articles where I use Go in coding competitions.

Problem

This problem is the first sample problem of the practice round. It is super easy, and designed to get you familiar with the Kickstart environment.

You have gathered N bags of candy and you want to distribute the candy amongst M kids. The i-th bag contains Ci pieces of candy. You want to make sure that every kid get the same amount of candy and that the number of pieces of candy they receive is the greatest possible. You can open each bag and mix all pieces of candy before distributing them to the kids.

How many pieces of candy will remain after you share the candy amongst kids, based on the rules described above?

Input

The first line of the input gives the number of test cases, T. T test cases follow.

Each test case consists of two lines. The first line of each test case contains two integers: integer N, the number of candy bags, and M, the number of kids.

The next line contains N non-negative integers C1,C2,…,CNC1,C2,…,CN representing array C, where the i-th integer represents the number of candies in the i-th bag.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of candies that will remain if you divide candies between kids according to the rules described above.

Limits

Time limit: 40 seconds.
Memory limit: 1 GB.

Test Set 1

1≤T≤1001≤T≤100.
1≤N≤1051≤N≤105.
1≤M≤1041≤M≤104.
0≤Ci≤10000≤Ci≤1000, for all i from 1 to N.

In Sample Case #1, we have N=7N=7 bags of candy. In total we have 1+2+3+4+5+6+7=281+2+3+4+5+6+7=28 candies that we want to divide between M=3M=3 kids. Every kid can get 99 pieces of candy, so 28−3×9=128−3×9=1 pieces of candy will remain.

In Sample Case #2, we have N=5N=5 bags of candy. In total we have 7+7+7+7+7=357+7+7+7+7=35 candies that we want to divide between M=10M=10 kids. Every kid can get 33 pieces of candy, so 35−10×3=535−10×3=5 pieces of candy will remain.

Solution

The solution to the problem is pretty straightforward. You get the total number of candies and modulo that by m. The result is passed to console output and must match the format that Google specifies.

package mainimport "fmt"func main() {
var numTests int
fmt.Scan(&numTests)

for i := 0; i < numTests; i++ {
var n int
var m int
var totalCandies int

fmt.Scan(&n)
fmt.Scan(&m)

for i:= 0; i < n; i++ {
var tempNum int
fmt.Scan(&tempNum)
totalCandies += tempNum
}

result := fmt.Sprintf("Case #%d: %d", i+1, totalCandies%m)
fmt.Println(result)

}

}

--

--