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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to find the minimum distance between two numbers in an array with Pascal

1 Answer

0 votes
program MinimumDistanceProgram;

{$mode objfpc}{$H+}

uses
  SysUtils;

type
  TIntArray = array of Integer;

{
  Finds the minimum distance between two target values in an array.

  Parameters:
    Numbers - Input array.
    Target1 - First target value.
    Target2 - Second target value.

  Returns:
    The minimum distance between the target values,
    or -1 if no valid pair exists.

  Time Complexity: O(n)
  Space Complexity: O(1)
}
function FindMinimumDistance(
  const Numbers: TIntArray;
  const Target1, Target2: Integer): Integer;

var
  CurrentIndex: Integer;
  LastSeenIndex: Integer;
  MinDistance: Integer;
begin
  { Special case: both targets are the same value }
  if Target1 = Target2 then
  begin
    LastSeenIndex := -1;
    MinDistance := MaxInt;

    for CurrentIndex := Low(Numbers) to High(Numbers) do
    begin
      if Numbers[CurrentIndex] <> Target1 then
        Continue;

      if (LastSeenIndex <> -1) and
          (Numbers[CurrentIndex] <> Numbers[LastSeenIndex]) then
      begin
          if (CurrentIndex - LastSeenIndex) < MinDistance then
              MinDistance := CurrentIndex - LastSeenIndex;
      end;

      LastSeenIndex := CurrentIndex;
    end;

    if MinDistance = MaxInt then
      Exit(-1);

    Exit(MinDistance);
  end;

  LastSeenIndex := -1;
  MinDistance := MaxInt;

  for CurrentIndex := Low(Numbers) to High(Numbers) do
  begin
    { Skip values that are not targets }
    if (Numbers[CurrentIndex] <> Target1) and
       (Numbers[CurrentIndex] <> Target2) then
      Continue;

    {
      If we previously encountered one target value and
      the current value is the other target value,
      update the minimum distance.
    }
    if (LastSeenIndex <> -1) and
       (Numbers[CurrentIndex] <> Numbers[LastSeenIndex]) then
    begin
      if (CurrentIndex - LastSeenIndex) < MinDistance then
          MinDistance := CurrentIndex - LastSeenIndex;
    end;

    { Remember the most recent occurrence }
    LastSeenIndex := CurrentIndex;
  end;

  if MinDistance = MaxInt then
    Result := -1
  else
    Result := MinDistance;
end;

var
  Numbers: TIntArray;
  Distance: Integer;

begin
  Numbers := TIntArray.Create(2, 3, 7, 3, 3, 4, 8, 8, 8, 11, 9, 1, 3);

  Distance := FindMinimumDistance(Numbers, 3, 8);

  if Distance >= 0 then
    WriteLn('Minimum distance between 3 and 8: ', Distance)
  else
    WriteLn('No valid pair found.');
end.


{
run:

Minimum distance between 3 and 8: 2

}

 



answered 13 hours ago by avibootz

Related questions

...