This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Baseball

Interview

Time limit1sMemory limit128 MB

Summary
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 NN cows (3≤N≤10003 \le N \le 1000) 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 (X,Y,Z)(X, Y, Z) complete two successful throws. Cow XX throws the ball to cow YY, who stands to the right of XX, and then cow YY throws the ball to cow ZZ, who stands to the right of YY. The second throw travels at least as far as the first one and no more than twice as far. Count the triples (X,Y,Z)(X, Y, Z) that Farmer John could have been watching.

Input

  • Line 1: the number of cows, NN.
  • Lines 2 to N+1N + 1: each line holds the position of one cow, an integer between 00 and 100 000 000100\,000\,000.

Output

  • Line 1: the number of triples (X,Y,Z)(X, Y, Z) where YY is to the right of XX, ZZ is to the right of YY, and the distance from YY to ZZ is at least the distance from XX to YY and at most twice that distance.

Hint

In the first example five cows stand at positions 33, 11, 1010, 77, 44. The four triples that work are the cows at positions (1,3,7)(1, 3, 7), (1,4,7)(1, 4, 7), (4,7,10)(4, 7, 10), (1,4,10)(1, 4, 10).

Examples1

  1. Example 1

    Input
    5
    3
    1
    10
    7
    4
    
    Expected output
    4