The key to this problem is this fact:
If the next greater permutation of X starts to differ from X from position i (common prefix from 0-i), then X[i+1:end] must be the greatest permutation for those elements. This means the elements i+1 to end must be in descending order. So to search for i, start from the end and search backwards for the first i where X[i]<X[i+1].