Problem Statement
You are given a int[] f that describes a set of values from a function f. The x-th element (zero-indexed) is the value of f(x). The function is not convex at a particular x value if there exist y and z values such that y < x < z, and the point (x, f(x)) lies strictly above the line between points (y, f(y)) and (z, f(z)) in the Cartesian plane. All x, y, and z values must be between 0 and n-1, inclusive, where n is the number of elements in f. Return the number of places where f is not convex.
Definition
- Class:
- ConvexityIssues
- Method:
- howMany
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int howMany(int[] values)
- (be sure your method is public)
Constraints
- f will contain between 3 and 50 elements, inclusive.
- Each element of f will be between -1000 and 1000, inclusive.
Examples
{0,1,4,9,16,25}Returns: 0
The function is convex at all given points.{0,2,2}Returns: 1
The point (1,2) is not below the line from (0,0) to (2,2).{1, 2, 3, 4, 5, 6, 7, 8}Returns: 0
{-1000, -501, 0, 500, 1000}Returns: 2
{-1000, 1, 1000}Returns: 1