Can anyone help me out with this problem?

Kuching the Cat enjoys eating fish. However, he accidentally bought a large fish and does not want to eat all of it. To solve his problem, he has subdivided the fish into N linear segments (head to tail) and has given each segment a ‘satisfaction rating’. These segments are labelled 1 to N. The higher the satisfaction rating, the more Kuching will enjoy eating this segment of the fish. However, fish only taste nice if eaten as a chunk. One chunk consists of multiple linear segments of fish that are contiguous/consecutive.

Kuching is very picky and will not enjoy a chunk of fish where the sum of satisfaction ratings is less than K (i.e. he wants the sum to be ≥ K). He wonders how many ways are there to cut a single chunk out of the fish such that he would enjoy eating. Those segments not part of the chunk will be thrown and not eaten.

To clarify: a chunk must contain at least 1 linear segment of fish and can only contain up to N segments in total (i.e. the entire fish).


The first line of input will contain two 32-bit signed integers, N and K. Note that N will always be positive while K can take both positive and non-positive integers.

The next line will be an array containing the satisfaction rating of the N segments. The i-th integer will be the satisfaction rating of the i-th segment of the fish. Do note that the satisfaction ratings will fit into a 32-bit signed integer and can take on positive and non-positive values.


Output a single integer, the number ways there are to cut a single chunk out of the fish such that Kuching the Cat would enjoy eating (i.e. sum of satisfaction ratings ≥ K).


5 2
1 -2 3 -4 5



Kuching the Cat has divided the fish into 5 segments. The first segment has satisfaction rating 1, 2nd has rating -2, 3rd has rating 3, 4th has rating -4 and the 5th has rating 5.

He will only enjoy chunks of fish which the sum of satisfaction ratings is more than or equal to 2.

Hence, the six chunks that he will enjoy are: [1 -2 3], [1 -2 3 -4 5], [-2 3 -4 5], [3], [3 -4 5], [5]

No other possible chunks will have a sum of satisfaction ratings ≥ K.

Do note that [1 3 5] is not a valid chunk as they are not contiguous/consecutive.

Problem adapted from Malaysia Computing Olympiad 2014

Note by Christopher Boo
6 years, 3 months ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link]( link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}


Sort by:

Top Newest

I've not solved this but to use DP, this is the key idea: you will need an estimated possible gain and a target.

You'll be creating a contiguous chunk from left to right.

When you pick up a piece such that the maximum possible gain on the rest of the pieces in the right will in no way satisfy the remaining target and that you are already short of the remaining target, you do not need to explore anymore to the right.

I understand that the above is poorly phrased. I will make a more complete solution if I have time.

Agnishom Chattopadhyay - 6 years, 3 months ago

Log in to reply

what you did?

SALMAN RASHEED - 6 years, 1 month ago

Log in to reply

You can always brute-force it. Check all possible 1 segment combinations, all possible 2 segment ones, and so on.

This seems like a DP problem, though. But I'm not familiar enough with DP to have any idea what to do to solve this problem.

Kp Govind - 6 years, 3 months ago

Log in to reply

For N=2311N=2^{31}-1 the complete search includes 2.3×1018\approx 2.3\times 10^{18} possibilities. This has to be solved via DP.

Jubayer Nirjhor - 6 years, 3 months ago

Log in to reply

@Christopher Boo , here is a complete python solution:

def xfish(data, target):
    chunks = []
    if data == []:
            return chunks
    if data[0] >= target:
    if sum(data) < target:
            return chunks
    for i in xfish(data[1:], target-data[0]):
    return chunks

def fish(data,target):
    chunks = []
    for i in xrange(len(data)):
            chunks += xfish(data[i:],target)
    return chunks


>>> fish([1, -2, 3, -4, 5],2)
[[1, -2, 3], [1, -2, 3, -4, 5], [-2, 3, -4, 5], [3], [3, -4, 5], [5]]

Agnishom Chattopadhyay - 6 years, 3 months ago

Log in to reply

Sorry, something is terribly wrong about the above. I do not know what.

Agnishom Chattopadhyay - 6 years, 3 months ago

Log in to reply


Problem Loading...

Note Loading...

Set Loading...