Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | Function to check if the given range of queries form an AP or not in the given array arr [ ] ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Print the result ; Otherwise ; Driver Code
def findAPSequence ( arr , N , Q , M ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 5 ) NEW_LINE i = 0 NEW_LINE while i + 1 < N : NEW_LINE INDENT j = i + 1 NEW_LINE while ( j + 1 < N and arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT for k in range ( i , j ) : NEW_LINE INDENT dp [ k ] = j - k NEW_LINE DEDENT i = j NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 ] NEW_LINE Q = [ [ 0 , 3 ] , [ 3 , 4 ] , [ 2 , 4 ] ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( Q ) NEW_LINE findAPSequence ( arr , N , Q , M ) NEW_LINE DEDENT