function check = pivot_sequence_check ( n, t )

%*****************************************************************************80
%
%% pivot_sequence_check() checks a pivot sequence.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 June 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer n: the number of steps in the pivot sequence.
%
%    integer t(n): a pivot sequence.
%
%  Output:
%
%    logical CHECK, error flag.
%    true, T is a pivot sequence.
%    false, T is not a legal pivot sequence.
%
  verbose = false;
  check = true;

  if ( n < 1 )
    if ( verbose )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'pivot_sequence_check(): Fatal error!\n' );
      fprintf ( 1, '  n < 1\n' );
    end
    check = false;
    return
  end

  for i = 1 : n

    if ( t(i) <= 0 )
      if ( verbose )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'pivot_sequence_check(): Fatal error!\n' );
        fprintf ( 1, '  t(%d) = %d <= 0\n', i, t(i) );
      end
      check = false;
      return
    elseif ( n + 1 - i < t(i) )
      if ( verbose )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'pivot_seq_check - Fatal error!\n' );
        fprintf ( 1, '  n + 1 - i = %d < t(%d) = %d\n', n + 1 - i, i, t(i) );
      end
      check = false;
      return
    end

  end

  return
end

