function check = bal_seq_check ( n, t )

%*****************************************************************************80
%
%% BAL_SEQ_CHECK checks a balanced sequence.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 November 2015
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Kreher, Douglas Simpson,
%    Combinatorial Algorithms,
%    CRC Press, 1998,
%    ISBN: 0-8493-3988-X,
%    LC: QA164.K73.
%
%  Parameters:
%
%    Input, integer N, the number of 0's (and 1's) in the sequence.
%    N must be positive.
%
%    Input, integer T(2*N), a balanced sequence.
%
%    Output, integer CHECK, error flag.
%    1, T is a legal balanced sequence.
%    0, T is not a legal balanced sequence.
%
  check = 1;

  if ( n < 1 )
    check = 0;
    return
  end

  one_count = 0;
  zero_count = 0;

  for i = 1 : 2 * n

    if ( t(i) == 0 )
      zero_count = zero_count + 1;
    elseif ( t(i) == 1 )
      one_count = one_count + 1;
    else
      check = 0;
      return
    end

    if ( zero_count < one_count )
      check = 0;
      return
    end

  end

  if ( one_count ~= zero_count )
    check = 0;
  end

  return
end
