Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find the longest increasing not sorted subsequence (LIS) of a sequence of numbers in Pascal

1 Answer

0 votes
program LongestIncreasingSubsequence;

const
  MaxSize = 100;

type
  TIntArray = array[1..MaxSize] of Integer;

function LIS(nums: TIntArray; n: Integer): Integer;
var
  length: TIntArray;
  i, j, maxLength: Integer;
begin
  for i := 1 to n do
    length[i] := 1;

  maxLength := 1;

  for i := 2 to n do
  begin
    for j := 1 to i - 1 do
    begin
      if nums[i] > nums[j] then
        if length[i] < length[j] + 1 then
          length[i] := length[j] + 1;
    end;
    if maxLength < length[i] then
      maxLength := length[i];
  end;

  LIS := maxLength;
end;

var
  nums: TIntArray;
  n, result: Integer;
begin
  nums[1] := 4;
  nums[2] := 5;
  nums[3] := 1;
  nums[4] := 10;
  nums[5] := 3;
  nums[6] := 9;
  nums[7] := 18;
  nums[8] := 19;
  n := 8;

  result := LIS(nums, n);
  WriteLn('Length of LIS: ', result);
end.



(*
run:

Length of LIS: 5

*)

 



answered Nov 7, 2025 by avibootz
...