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

  • SEARCH
  • Home
  • 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 8570413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:33:08+00:00 2026-06-11T18:33:08+00:00

I have number array: 16,17,19,19,20. I need to find a missing number/gap (in this

  • 0

I have number array: 16,17,19,19,20.
I need to find a missing number/gap (in this case it is 18/one number but it may be two numbers e.g.
16,17,20,21) and then i want to fill the gap in a way that rest of the array moves one (x) numbers up.
This array can have more missing numbers (gap) e.g. 16,17,19,19,20,21,23,23.
I have this loop but there is problem – see comment:

    <?php
      $ar = array(16,17,19,19,20);
                $action = false;
                $new = array();
                $temp = array();

        foreach ( $ar   as $k => $v ) {
                if ( $k == 0 )
                    {
                      // case 0 - insert first value of var into array - never need to change
                        $new[] = $v; 
                    }
                elseif ( $k > 0 ) 
                    { 
                      if ( end($new) + 1 == $v ) 
                        { 
                      // case 1 - numbers are consequence/no gap - insert into array - no change 
                            $new[] = $v;
                        }

                  elseif ( end($new) + 1 != $v )
                            {
                              // case 2 - there is a gap: find the size of the gap (1,2,x) and then subtract all next values of array with the gap
                                    $gap = $v - end($new) - 1 ; // find value of the gap 
                                    //echo "<br> gap is: " . $gap; // PROBLEM - gap get increased by every loop but i need to keep gap size static and process rest of the array
                                    $action = true;
                                            if ( $action = true )
                                                {    
                                                        $temp[] = $v - $gap;
                                                }
                                        }
                            }
                        }                            

echo "<br>";            
print_r ( $new );
echo "<br>";
print_r ( $temp );

so the result is:
array new is ok

    Array ( [0] => 16 [1] => 17 ) 

array temp is not ok

    Array ( [0] => 18 [1] => 18 [2] => 18 ) 

it should be 18,18,19

How is this scenario addressed?
Thanks.

  • 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-06-11T18:33:09+00:00Added an answer on June 11, 2026 at 6:33 pm

    try this, you can make any number of gaps and sequences, and recursion will find the size of gap and shift numbers up, then look for another gap/gaps.

    <?php
    
    $ar = array(16,17,17,19,19,20,20,24,24);
    
    echo '<pre>Initial:<br>';
    
    print_r ($ar);
    
    echo '<br>Fixed:<br>';
    
    function find_gaps($array) {  
    
                    $key = 0;
                    $first_half = array();
                    $gap = 0;                   
                    $gaps = array();
                    $action = false;
                    $new = array();  // helper array
                    $second_half = array();
    
        foreach ( $array as $k => $v ) {
                    if ( $k == 0 )  // first number in line - no need to change
                        {
                            $new[] = $v;  
                            $first_half[$k] = $v;
                        }
                    elseif ( $k > 0 )  // next numbers 
                        { 
                          if ( end($new) + 1 == $v ) // if first member of array is consequent of helper .. 
                              { 
                                $new[] = $v;        
                                $first_half[$k] = $v;  // .. it's ok- just make new array
                                }
                          elseif ( end($new) == $v )  // if there are two or more same numbers - it's ok ..
                                    { 
                                $new[] = $v;
                                $first_half[$k] = $v;  // .. no action-just keep adding into array 
                               }
                        elseif ( end($new) + 1 != $v )  // if last value of helper array is not 1 less of our array - there is a gap
                                    {
                                            $gap = $v - end($new) - 1 ; // find size of the gap in numbers ( can be 1,2,3..)
                                            //echo $gap; break;
                                            $gaps[] = $gap;  // put gap into array - it will get increased but you need only first value - see if ($action)
                                            $action = true;
                                    }   
                                        if ( $action )
                                                { 
                                                $second_half[$k] = $v;  // make second half array
                                                } 
                      }
                } // end array  loop then..
    
    
        $second_half_fixed = array();
    
            foreach ( $second_half as $keys => $values )
                              $second_half_fixed[$keys] = $values - $gaps[0];
    
                $a['first']  = $first_half;
                $a['fixed'] = $second_half_fixed;
                $b['fixed'] = $first_half + $second_half_fixed;
                $b['gaps'] = $gaps;
    
                $count_gaps = count($b['gaps']);
    
                if ($count_gaps == 0 OR $count_gaps == 1 ){
                                return $b;
                    }
                else // more gaps
                        {
                            echo 'run again<br>';
                              return ( find_gaps($b['fixed'])) ;            
                        }       
        } // end function 'find_gaps'
    
        $fixed = find_gaps($ar);
    
        print_r ( $fixed['fixed'] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an int array and I need to find the number of elements
I have an array of phone numbers and I need to find if a
I have an array in jQuery, and I need to count the number of
I have an array of phone numbers, and I need a fast way to
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
I have toyed with a number of ideas to do this, but so far
I have an associative array and I need to find the numeric position of
Given an array that has the numbers {0......2^k -1} except for one number ,
I have looked for answer on other questions but I can't find one. My
I have a number of byte[] array variables I need to convert to string

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.