Challenge 4 (Year 2 Sem 1)

Challenge 4 is the fourth challenge in year 2 semester 1 and the whole of year 2. It is a "boss fight" and is a challenge on loops. The challenge is notorious for its difficulty, and at the time of writing (June 2021) there is only 20 people who completed it. Unlike other challenges, this challenge has no end date.

Description
''You've come a long way. Challenge 1, 2 and even 3 fell before you. Now it is time for you to meet some of the hardest that this chapter has to offer. Brace yourself for the BOSS FIGHT!''

WARNING: The questions in this training may be very hard. Don't go insane completing it.

You may also require out-of-syllabus knowledge or content that is covered in later chapters, but it will be kept to a minimum.

For some of the problems the input size may be very large, and thus it is possible for your code to take a long time to process and produce the result. If your code works well on small-size test case but still fail the grader, it is possible that your code is too inefficient to complete larger scenarios within a reasonable amount of time. If that happens, try to rethink your coding strategy to come up with something more efficient. This challenge is not a prerequisite for Challenge 5 and 6, no need to rush to complete it, spend time and think how to improve your solution.

Good luck!

Prerequisites

 * Complete Challenge 3: Loops
 * Complete Lab 6.2: Loops
 * Complete Review 6.2: Loops

Unlocked after completion

 * Intern of the Year (Score at least 60% for Challenge 4: Loops [BOSS FIGHT])
 * Loopy Master (Score at least 100% for Challenge 4: Loops [BOSS FIGHT])

Question
You are given a sequence of integers. Your job is to take a continuous chunk of this sequence, such that the sum of its elements is maximized. You only need to return the maximum sum attained.

For example, suppose you are given the sequence (3, -10, 4, -1, 2, 3, 6, -7). You get the maximum sum by taking the elements (4, -1, 2, 3, 6) which sums to 14.

Note: It is possible to take a chunk of zero elements. In this case the sum is 0.

There are up to 10000 integers in each sequence.

Solution
Use Kadane's algorithm. Don't copy the code on Wikipedia!

Question
So you thought you saw the last of that question? FOOL! BEHOLD MY FULL POWER AND D̪̭͖E͙͚̺S̡͙̜P̺̠̝A̯̦̼I͕̦̟R͖̟̪!

This problem is almost exactly the same as the previous one (i.e. Challenge 3 Q4). You may want to refer to what you have submitted in Challenge 3 to help you.

Once again, the function  needs to return Z, the value at which   returns 0. Z is guaranteed to be between 1 and –REDACTED–.

What? No upper bound at all? No.

Solution
You need to find a way to obtain the upper bound first (hint: powers of 2), before performing binary search.

Question 3: Selection
Note that this was previously a question in Challenge 6, known as 4sum.

Question
In this problem, you are provided with four sequences of integers, a, b, c, and d. Your task consists of selecting one integer from each sequence, such that their sum is 0. You can assume that exactly one such selection exists.

For example:

a = (5, 17, -8)

b = (-13, 19)

c = (6, -9, 10, 0)

d = (-14, 7)

You can pick 17 from a, -13 from b, 10 from c and -14 from d to add up to 0. In this case, at the bottom of the function you should assign:

ans_a = 17

ans_b = -13

ans_c = 10

ans_d = -14

These answers will then be packaged into another sequence (at the return line) for grading.

There are up to 500 integers in each sequence, each between -20000 and 20000 inclusive.

Solution
You will need to brute force smarter. Make a container of all possible sums of one element in a and one element in b, and do the same for c and d. The rest should be easy.