Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:31+00:00 2026-05-27T14:52:31+00:00

Consider I have this array: $array = array( ‘word1’, ‘abc’, ‘abc’, ‘word2’, [other words]

  • 0

Consider I have this array:

$array = array(

'word1',
'abc',
'abc',
'word2',
 [other words]
'word1',
'dfg'
'word2',
 [other words]
);

I need to find the minimum distance between 2 given words. (let ‘word1’ and ‘word2’ be these 2 words)

In this case the minium distance between word1 and word2 is 1 because in the second group of words they are separated by only ‘dfg’.

I wrote a simple code but it’s too expsensive and I am looking for a faster version.

//> PSEUDO CODE
function minDistance( $words, $word1, $word2 ) {
    foreach( $words as $k=>$v) 
      if ( $v == $words1 )
         $positionsOfFirstWord[] = $k;

      if ( $v == $words2 )
         $positionsOfSecondWord[] = $k;


     //> If word1 or word2 was not found in the array then
     //> return max distance possibile (count($words))

     //> Now we have 2 array containg the position of both word we need.

     foreach( $positionsOfFirstWord as $v )
        foreach( $positionsOfSecondWord as $vv )
          $distance = abs($vv-$v);

}

Note the order of words in $array isn’t important (that’s why there is abs())

Do you think there could be a better version?

Please note the function must return 1 in this case too:

array(
 [other words]
'word2',
'dfg',
'word1'
 [other words]
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:52:32+00:00Added an answer on May 27, 2026 at 2:52 pm

    I think a simple loop is enough. Keep track over the current minimum and of the last word1 and update current minimum if a word2 is found. Basically you are utilizing the fact that a word2 will always be closest to the last word1 found

     let minimum = INFINITY
     let lastword1 = -1
     let lastword2 =  -1
     foreach word w in words
     {
    
          if ( w is word1 )
          {
               lastword1 = current position;
    
               find distance between lastword2 and w update minimum if needed
          }
    
          if ( w is word2 )
          {
              lastword2 = current position;
    
              find distance between lastword1 and w update minimum if needed
          }
    
     }
    

    You can do this in O(n) but there might faster ways if pre processing can be done and you need to answer multiple queries

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the question sounds silly, but consider this: I have an array of
Consider this page @ http://www.bloodbone.ws/screwed.html I need to be able to have the a.grow
Consider I have an anchor which looks like this <div class=res> <a href=~/Resumes/Resumes1271354404687.docx> ~/Resumes/Resumes1271354404687.docx
This may seem a very silly question. Consider this: I have a simple Boolean
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this scenario. I have my own website, that I use as my identifier,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider a grocery store scenario (I'm making this up) where you have FACT records
Is it possible to have nested set capabilities in this somewhat custom setup? Consider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.