In: Computer Science
Given an array, return the element at which the "pattern" breaks.
For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3.
PLEASE USE DIVIDE AND CONQUER. Thank you.
Python please or just a description of the algorithm
To check if the pattern break in the given array, we need to check the array from starting position to the end of the array because the patter may break at any position in the array. If the pattern match in the half of the array then we need to check another half of the array as well because it may break in another half.
#import module
import numpy as np
#method to check if pattern break in the given array
def checkPattern(array):
diff = array[1] - array[0]
for i in range(len(array)-1):
#check if pattern break
if diff != (array[i+1] - array[i]):
return array[i+1]
#array declaration and initialization
array = np.array([2,4,6,8,11,13,15,17])
#display the result
print(checkArray(array))
The screenshot of the above source code is given below:
OUTPUT:
11