function t = pivot_sequence_successor ( t )

%*****************************************************************************80
%
%% pivot_sequence_successor() computes the pivot sequence successor.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 June 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer t(n): the previous pivot sequence.
%    To initiate the routine, call with t=linspace(n,1,n).
%
%  Output:
%
%    integer t(n): the lexical successor of the input.
%
%
%  Check.
%
  check = pivot_sequence_check ( t );

  if ( ~ check )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'pivot_sequence_successor(): Fatal error!\n' );
    fprintf ( 1, '  The input array is illegal.\n' );
    error ( 'pivot_sequence_successor(): Fatal error!' );
  end
%
%  Find last entry that is less than its maximum value.
%
  n = length ( t );

  last = 0;
  for i = n : -1 : 1
    if ( t(i) < n + 1 - i )
      last = i;
      break;
    end
  end

  if ( last == 0 )
    t = ones ( 1, n );
  else
    t(last) = t(last) + 1;
    t(last+1:n) = 1;
  end

  return
end
