Problem Statement
NOTE: This problem statement contains subscripts/superscripts that may not display properly if viewed outside of the applet.
For positive integers h and r (1 <= r <= h) we define sequence Fh,r as {1, 2, 3, ..., h-1, h, h-1, h-2, ..., r+1, r}. Let S(Fh,r) be the the sum of all numbers in Fh,r and N(Fh,r) - the length of Fh,r.
For example, F3,2 is {1, 2, 3, 2}, S(F3,2) = 1 + 2 + 3 + 2 = 8, and N(F3,2) = 4. F5,5 is {1, 2, 3, 4, 5}, S(F5,5) = 15, and N(F5,5) = 5.
You are given a long sum. Return minimal possible N(Fi,j) such that S(Fi,j) = sum. If there is no such sequence Fi,j, return -1 instead.
Definition
- Class:
- AnEasyProblem
- Method:
- solve
- Parameters:
- long
- Returns:
- int
- Method signature:
- int solve(long sum)
- (be sure your method is public)
Constraints
- sum will be between 1 and 1,000,000,000,000(10^12).
Examples
6
Returns: 3
When h = r = 3, S(Fh,r) = 6 and N(Fh,r) = 3. There is no other h,r satisfing S(Fh,r) = 6. So you should return 3.7
Returns: -1
100
Returns: 15
1000000000000
Returns: 1428571