Cow Baseball
InterviewTime limit1sMemory limit128 MB
Count triples of cows in increasing position whose second gap is at least the first gap and at most twice it.
- Level
Medium5 of 10
- Topics
- Two pointers, Sorting
- Solved
- No attempts yet
Problem
Farmer John's cows () stand in a row on the number line, and no two of them stand at the same position. They are practicing throws with a baseball before an important game against the cows on the neighboring farm.
While Farmer John watches, three cows complete two successful throws. Cow throws the ball to cow , who stands to the right of , and then cow throws the ball to cow , who stands to the right of . The second throw travels at least as far as the first one and no more than twice as far. Count the triples that Farmer John could have been watching.
Input
- Line 1: the number of cows, .
- Lines 2 to : each line holds the position of one cow, an integer between and .
Output
- Line 1: the number of triples where is to the right of , is to the right of , and the distance from to is at least the distance from to and at most twice that distance.
Hint
In the first example five cows stand at positions , , , , . The four triples that work are the cows at positions , , , .