Python - K difference Consecutive Element


The K difference is set to any value that will be used to set the difference between two numbers. The consecutive element is those elements that follow the sequential order. In Python, we have some built-in functions such as range(), len(), abs(), append(), and, sort() will be used to solve the K difference Consecutive element.

Let’s take an example of this.

The given list, [5, 6, 3, 2, 4, 3, 4]

The K value set to 1 means each consecutive elements follow the differences of 5.

Then the final result becomes,

[True, False, True, False, True, True]

Explanation:

The outcome comes true when elements differ by 1 otherwise False.

Syntax

The following syntax is used in the examples-

range()

The built-in function range() is defined by returning the order of numbers according to the given length.

len()

The len() is an in-built function in Python that returns the length of the object.

abs()

The built-in function abs() returns the absolute value of the given input.

append()

The append() is an in-built function in Python that can be used to add the element at the end of the list.

sort()

The sort() is an in-built function in Python that sort the list in sequence of ascending order.

Using Brute Force

In the following example, we will use the brute force method that finds the possible solution for K difference Consecutive elements. In this program, there will be using for loop that set the iteration based on length of the list. Next, using an if-statement it will set the equivalence condition between absolute length of the list and K by using == operator. If the K value satisfies the specific result then it will append the value true to a consecutive element otherwise false. Finally, the function returns the specified result.

Example

def k_diff_element(l, K):
# Empty list to store the final output
    f_res = []
# condition for the consecutive element
    for i in range(len(l) - 1):
        if abs(l[i] - l[i+1]) == K:
            f_res.append(True)
        else:
            f_res.append(False)
    return f_res
# create the list
lst = [5, 6, 3, 2, 5, 3, 4]
K = 1
res = k_diff_element(lst, K)
print("The result of K difference consecutive elements:\n", res)

Output

The result of K difference consecutive elements: 
[True, False, True, False, False, True]

Using sort() Function

In the following example, the program uses a recursive function to accept two parameters- input list and K value. Using the built-in function sort() to a given list will sort the elements in ascending order. Then create the empty list in the variable result f_res that will store the final output of the program. Next, use the for loop where variable i iterates through the given input list using the built-in functions len() and range(). Then use the if-else statement to set the condition for consecutive differences of elements, if the K difference satisfies it results as true otherwise false. Finally, it will use the function return to specify the output.

Example

def k_diff_element(l, K):
    l.sort()
    f_res = []
# condition for the consecutive element
    for i in range(1, len(l)):
        if l[i] - l[i-1] == K:
            f_res.append(True)
        else:
            f_res.append(False)
    return f_res
# create the list
lst = [5, 6, 3, 2, 4, 3, 4]
K = 1
res = k_diff_element(lst, K)
print(res)

Output

 [True, False, True, False, True, True]

Using two Pointers

In the following example, the program uses the two pointers that means it follows two directions in the given list such as left and right. Then the recursive function accepts two parameters- l and K to receive the value from the input list and K value respectively. Now use the built-in function sort() that sort the integer list in ascending order. Then initialize the initial value to left and right pointer that will be used to calculate the difference between consecutive elements. Next, the empty list is stored in the variable result. Moving ahead to set the while loop where it set the condition of variable right with length of given list using < operator and calculate the difference of l[right] and l[left] to store in the variable diff. Using if else-if ladder it set the conditions based on the left and right difference of the list and returns the variable result to get the output.

Example

def k_diff_element(l, K):
    l.sort()
# Set the first initial pointer
    left = 0
# Set the Second initial pointer
    right = 1
    result = []
    while right < len(l):
        diff = l[right] - l[left]
        if diff == K:
            result.append(True)
            left += 1
            right += 1
        elif diff < K:
            right += 1
        else:
            left += 1
    return result
# create the list
lst = [5, 7, 2, 3, 4]
K = 1
res = k_diff_element(lst, K)
print(res)

Output

[True, True, True]

Conclusion

We learned the concept of K difference Consecutive elements which sets the difference value to check the sequence order of the given list. There are differences in all the output according to the given input list and specific conditions and operations. This type of program is generally used to solve the algorithm-problem statement.

Updated on: 16-Aug-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements