Home  >  Article  >  Backend Development  >  How to find non-consecutive numbers in a sequence of numbers in php

How to find non-consecutive numbers in a sequence of numbers in php

PHPz
PHPzOriginal
2023-04-05 10:31:22746browse

In PHP development, it is often necessary to find discontinuous numbers in a sequence of numbers. How to realize this requirement quickly and efficiently? This article will explain it to you in detail.

1. Problem background

Find discontinuous numbers in a sequence of numbers, that is, find the numbers that have a certain interval from the next number after a certain number. For example, given a sequence [1, 2, 6, 7, 9, 12, 15, 17] and asked to find discontinuous numbers, assuming the interval is 4, the return value is [2, 9, 17].

2. Problem Analysis

To realize this requirement, we need to traverse the entire number sequence and do the following processing for each number:

  1. Check the current number and the previous number Whether the difference of a number is equal to the specified interval. If equal, it indicates that the number is one of the discontinuous numbers; if not equal, the number is recorded as the current number.
  2. Add the recorded numbers to a result array, and finally return the result array.

In specific implementation, the following methods can be used:

  1. Define a $result array to store discontinuous numbers.
  2. Define a $previous variable to record the previous number.
  3. Traverse the sequence of numbers and process each number.

    1. If the difference between the number and the previous number is equal to the specified interval, the number is added to the $result array;
    2. Otherwise, the number is recorded as $previous.
  4. Return $result array.

The specific implementation code is as follows:

function findDiscontinuousNumbers($nums, $interval) {
    $result = [];
    $previous = null;
    foreach ($nums as $num) {
        if (!is_null($previous) && $num - $previous == $interval) {
            $result[] = $num;
        }
        $previous = $num;
    }
    return $result;
}

$nums = [1, 2, 6, 7, 9, 12, 15, 17];
$interval = 4;
$result = findDiscontinuousNumbers($nums, $interval);
print_r($result);

3. Code optimization

The above implementation can already meet the requirements, but it may not be efficient in actual use. . Consider the following optimization:

  1. When a number has been recorded as a discontinuous number, the following numbers cannot be continuous with it, so $previous can be set to this discontinuous number before the next processing .
  2. For the search of digital sequences with large differences, during the traversal process, the position of the last discontinuous number can be recorded, and the next search can be processed directly from this position, which can reduce unnecessary traversals.

The optimized code is as follows:

function findDiscontinuousNumbers($nums, $interval) {
    $result = [];
    $previous = null;
    $last_discontinuous_index = null; // 上一次不连续数字的索引位置
    for ($i = 0; $i < count($nums); ) {
        if (!is_null($previous)) {
            if ($nums[$i] - $previous == $interval) {
                $result[] = $nums[$i];
            } else {
                $previous = $nums[$i];
                $last_discontinuous_index = $i;
            }
        } else {
            $previous = $nums[$i];
            $last_discontinuous_index = $i;
        }
        $i += ($i == $last_discontinuous_index + 1) ? 1 : $interval;
    }
    return $result;
}

$nums = [1, 2, 6, 7, 9, 12, 15, 17];
$interval = 4;
$result = findDiscontinuousNumbers($nums, $interval);
print_r($result);

IV. Summary

This article briefly introduces the method of finding discontinuous numbers in PHP and gives Basic implementation. In actual use, appropriate implementation methods and optimization measures should be selected according to different needs to achieve better performance and effects.

The above is the detailed content of How to find non-consecutive numbers in a sequence of numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn