Related: Splitting a polygon - removing concave points to create convex polygons
I am looking for an algorithm to do the following:
The input is an array of two-dimensional points (P 0 ... P N-1 ). The length N of the array changes (3 ≤ N <∞)
For any M ≤ N, there may or may not be a convex polygon whose vertices are P 0 ... P M-1 in some order.
Note edges are not necessarily adjacent pairs in an array.
What is the most efficient algorithm for finding the maximum of M such that this convex polygon exists?
My current algorithm is very inefficient. I test with M = 3, then M = 4, M = 5, etc., Calculate the body, then check that all P 0 ... P M-1 are the vertices of the body, if not, then I exit the loop and return the M-1.
Example # 1: [(-2,2), (2,2), (-2,-2), (-1,1)]

result: 3 (because the first three points form a triangle, but adding P 3 = (-1,1) will make the polygon non-convex)
Example # 2: [(-2,2), (2,2), (-2,-2), (1,-1)]

result: 4 (since a convex quadrilateral can be built from all 4 points in the array)
Refresh Example # 3: [(-3,3), (3,3), (2,-1), (-3,-3), (3,-3), (-2,1)] 
result: 4.
This example shows why it is not enough to take the convex hull of all the set points and find the prefix that is its subset. (3,-3) cannot be part of a convex polygon consisting of the first five points, because the previous point (2,-1) will no longer lie on the body. But this is (3,-3) , which should be rejected, although it lies on the body of all six points, but (2,-1) does not.
Examples of invalid entries:
[(-1,-1), (0,0)] (too few points)[(-1,-1), (0,0), (1,1), (1, -1)] (the first three points are collinear: I would not expect the algorithm to handle this.)