What is the way to find if an array contains an arithmetic progression (sequence)

I sorted an array of numbers like

1, 4, 5 , 6, 8

What is the way to know if this array contains an arithmetic progression (sequence)?

as in this example

4,6,8

or

 4,5,6

Note: minimum numbers in sequence 3

+3
source share
6 answers

You can solve this problem recursively by breaking it into smaller tasks that:

  • Define the pairs {1,4}, {1,5} ... {6,8}
  • For each pair, find sequences with the same interval

First create scaffolding to fix the problems:

Dim number(7) As Integer
Dim result() As Integer
Dim numbers As Integer
Sub FindThem()
number(1) = 1
number(2) = 4
number(3) = 5
number(4) = 6
number(5) = 8
number(6) = 10
number(7) = 15
numbers = UBound(number)
ReDim result(numbers)
Dim i As Integer
For i = 1 To numbers - 2
    FindPairs i
Next
End Sub

Now iterating over pairs

Sub FindPairs(start As Integer)
    Dim delta As Integer
    Dim j As Integer
    result(1) = number(start)
    For j = start + 1 To numbers
        result(2) = number(j)
        delta = result(2) - result(1)
        FindMore j, 2, delta
    Next
End Sub

Search for sequences when you go

Sub FindMore(start As Integer, count As Integer, delta As Integer)
    Dim k As Integer
    For k = start + 1 To numbers
        step = number(k) - result(count)
        result(count + 1) = number(k) ' should be after the if statement
                                      ' but here makes debugging easier
        If step = delta Then
            PrintSeq "Found ", count + 1
            FindMore k, count + 1, delta
        ElseIf step > delta Then ' Pointless to search further
            Exit Sub
        End If
    Next
End Sub

It is just to show results.

Sub PrintSeq(text As String, count As Integer)
    ans = ""
    For t = 1 To count
        ans = ans & "," & result(t)
    Next
    ans = text & " " & Mid(ans, 2)
    Debug.Print ans
End Sub

findthem
Found  1,8,15
Found  4,5,6
Found  4,6,8
Found  4,6,8,10
Found  5,10,15
Found  6,8,10

: , , , !

+2

, , :

- 2 , , 1- 2- . , 2 , , .

3 , , a [i] [j], j > + 1 , - , ] i, j [.

+1

-, , .

a[i] , a[i+n] - .

, , . , x - , y - , x + i*(y-x), = 0. x + 2 * (y-x). . , !

= 3, = 4 .., , .

l - , i 0 l-2, n 0 l-i-1

, 4,6,8, 6,8. , . , . , .

+1

, a_1, a_2, , , - . 3 , .

progression (A, n)
   for i = 1 ... n - 2
      a_1 = A[i]
      for j = i + 1 ... n - 1
         a_2 = A[j]
         d = a_2 - a_1
         S = [ i, j ]
         for k = j + 1 ... n
            if ( d == ( a[k] - a[S.last] ) )
               /* Append the element index to the sequence so far. */
               S += k
         if ( |s| > 2 )
            /* We define a progression to have at least 3 numbers. */
            return true
   return false

S , A. O (n ^ 3), S .

, ...

+1

, , .

, ,

like:

for ($i = 1; $i <= $countArray - 2; $i++) {
     for ($j = $i+1; $j <= $countArray - 1; $j++) {         
    for ($k = $j+1; $k <= $countArray; $k++) {
            if($array[$j]-$array[$i] == $array[$k]-$array[$j]){
             # found 
            }
        }
   }
 }
0
source

Here is the code in Swift 4:

extension Array where Element == Int {

    var isArithmeticSequence: Bool {
        let difference = self[1] - self[0]
        for (index, _) in self.enumerated() {
            if index < self.count-1 {
                if self[index + 1] - self[index] != difference {
                    return false
                }
            }
        }
        return true
    }

    var arithmeticSlices: [[Int]] {

        var arithmeticSlices = [[Int]]()
        var sliceSize = 3
        while sliceSize < self.count+1 {

            for (index, _) in self.enumerated() {

                if (index + sliceSize-1) <= self.count - 1 {

                    let currentSlice = Array(self[index...index + sliceSize-1])
                    if currentSlice.isArithmeticSequence {
                        arithmeticSlices.append(currentSlice)
                    }
                }
            }
            sliceSize+=1
        }
        return arithmeticSlices
    }
}


let A = [23, 24, 98, 1, 2, 5]
print(A.arithmeticSlices) // []

let B = [4, 7, 10, 4,5]
print(B.arithmeticSlices) //[[1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], [1, 2, 3, 4, 5]]

let C = [4, 7, 10, 23, 11, 12, 13]
print(C.arithmeticSlices) // [[4, 7, 10], [11, 12, 13]]
0
source

Source: https://habr.com/ru/post/1772599/


All Articles