program CheckMatrixRows;
const
Rows = 3;
Cols = 3;
type
TMatrix = array[1..Rows, 1..Cols] of Integer;
function HasUniqueNumbersInRow(const Row: array of Integer): Boolean;
var
Seen: array of Boolean;
i, MaxValue: Integer;
begin
// Find the actual maximum number in the row
MaxValue := Row[0];
for i := 1 to High(Row) do
if Row[i] > MaxValue then
MaxValue := Row[i];
// Allocate memory safely based on the maximum value in the row
SetLength(Seen, MaxValue + 1);
// Initialize Seen array to False
for i := 0 to High(Seen) do
Seen[i] := False;
for i := 0 to High(Row) do
begin
if Seen[Row[i]] then
begin
HasUniqueNumbersInRow := False;
Exit;
end;
Seen[Row[i]] := True;
end;
HasUniqueNumbersInRow := True;
end;
function CheckMatrixRows(const Matrix: TMatrix): Boolean;
var
i, j: Integer;
Row: array of Integer;
begin
for i := 1 to Rows do
begin
SetLength(Row, Cols);
for j := 1 to Cols do
Row[j - 1] := Matrix[i, j];
if not HasUniqueNumbersInRow(Row) then
begin
CheckMatrixRows := False;
Exit;
end;
end;
CheckMatrixRows := True;
end;
var
Matrix: TMatrix = (
(1, 2, 3),
(4, 5, 6),
(7, 8, 7)
);
begin
if CheckMatrixRows(Matrix) then
WriteLn('All rows contain unique numbers.')
else
WriteLn('Some rows contain repeated numbers.');
end.
(*
run:
Some rows contain repeated numbers.
*)