Building a Fence
InterviewTime limit1sMemory limit128 MB
Count the ordered ways to cut a plank of length N into four positive integer pieces whose longest piece is strictly shorter than the other three combined.
- Level
Medium5 of 10
- Topics
- Combinatorics, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
Farmer John wants to build a four-sided fence to enclose his cows. He has a single plank of wood of integer length (). He will cut it at three points to produce four pieces, each of positive integer length.
The four pieces may have any positive integer lengths, as long as they can be arranged into a quadrilateral fence with strictly positive area. In how many different ways can he cut the plank so that the four resulting pieces form a valid fence?
Notes:
- Two ways of cutting are different if one has a cut at a position where the other does not. Do not eliminate rotations, reflections, or other symmetric duplicates.
- The fence must enclose an area greater than .
- The answer always fits in a signed 32-bit integer.
Input
A single integer .
Output
A single integer: the number of ways to cut the plank into four pieces that form a quadrilateral with positive area.
Hint
For , there are ways to cut the plank into four ordered pieces: , , , , , , , , , and . Four of them — , , , and — cannot form a quadrilateral, because one side is as long as the other three combined. That leaves valid ways.
Four lengths form a quadrilateral with positive area if and only if the longest piece is strictly shorter than the sum of the other three.