Imports System
Imports System.Collections.Generic
Public Class ArraySortedAndValidGap
Public Shared Function IsArraySortedAndValidGap(ByVal list As List(Of Integer)) As Boolean
If list.Count < 2 Then Return True
Dim increasing As Boolean = list(1) > list(0)
For i As Integer = 1 To list.Count - 1
Dim diff As Integer = list(i) - list(i - 1)
If diff <> 1 AndAlso diff <> 2 AndAlso diff <> 3 AndAlso diff <> -1 AndAlso diff <> -2 AndAlso diff <> -3 Then
Return False
End If
If (increasing AndAlso diff <= 0) OrElse (Not increasing AndAlso diff >= 0) Then
Return False
End If
Next
Return True
End Function
Public Shared Sub Main()
Dim list1 As List(Of Integer) = New List(Of Integer) From {
1,
2,
3,
5,
8,
11,
14,
15
}
If IsArraySortedAndValidGap(list1) Then
Console.WriteLine("List is sorted and has valid gaps")
Else
Console.WriteLine("List is not sorted or gaps are invalid")
End If
Dim list2 As List(Of Integer) = New List(Of Integer) From {
15,
14,
11,
8,
5,
3,
2,
1
}
If IsArraySortedAndValidGap(list2) Then
Console.WriteLine("List is sorted and has valid gaps")
Else
Console.WriteLine("List is not sorted or gaps are invalid")
End If
End Sub
End Class
' run:
'
' List is sorted and has valid gaps
' List is sorted and has valid gaps
'