Python program to test if all elements in list are maximum of K apart


In many programming scenarios, we come across situations where we need to determine if all elements in a list are maximum of K positions apart. This problem arises in various domains, such as data analysis, sequence processing, and algorithmic challenges. Being able to test and validate such conditions is essential for ensuring the integrity and correctness of our programs.

In this article, we will explore a Python program to solve this problem efficiently. We'll discuss the concept, present a step-by-step approach to tackle the problem, and provide a working code implementation. By the end of this article, you will have a clear understanding of how to check if elements in a list are maximum of K positions apart using Python.

Understanding the Problem

Before we dive into the solution, let's first understand the problem statement in detail.

Given a list of elements, we need to determine if each element is at most K positions away from any other maximum element in the list. In other words, we want to check if the maximum element of the list and any other maximum element(s) are within a distance of K positions.

To illustrate this, consider the following example −

Input: [2, 7, 4, 9, 5, 1]
K = 2

In this case, the maximum element is 9, and it is at a distance of 2 positions or less from the other maximum element(s), which are 7. Therefore, the condition is satisfied.

Now, let's take another example 

Input: [3, 8, 6, 1, 9, 2]
K = 3

In this case, the maximum element is 9, but it is at a distance of 4 positions from the next maximum element, which is 8. Hence, the condition is not satisfied.

Our task is to write a Python program that takes a list of elements and a value for K as input and returns whether the given condition is met or not.

Approach and Algorithm

To solve this problem, we can follow a simple approach that involves iterating over the list and comparing each element with the maximum element found so far. We will keep track of the maximum element and its position as we iterate through the list.

Here's a step-by-step algorithm to implement our approach −

  • Initialize two variables −

    • max_element to store the maximum element found so far (initialized to the first element of the list).

    • max_index to store the index of the maximum element (initialized to 0).

  • Iterate through the remaining elements of the list from index 1 to n-1.

    If the current element is greater than max_element, update max_element to the current element and max_index to the current index.

  • After the iteration, we have the maximum element and its index in the list.

  • Now, iterate through the list again and for each element, check if it is the maximum element or if it is at most K positions away from the maximum element. If any element fails this condition, return False.

  • If all elements pass the condition, return True.

By following this approach, we can efficiently determine if all elements in the list are at most K positions away from any other maximum element.

In the next section, we will implement this algorithm in Python.

Implementation

Now, let's implement the algorithm discussed in the previous section using Python code. Below is the code snippet that demonstrates the solution 

def test_k_apart(nums, k):
   max_element = nums[0]
   max_index = 0

   # Find the maximum element and its index
   for i in range(1, len(nums)):
      if nums[i] > max_element:
         max_element = nums[i]
         max_index = i

   # Check if all elements are at most K positions away
   for i in range(len(nums)):
      if nums[i] != max_element and abs(i - max_index) > k:
         return False

   return True

In this code, we define a function test_k_apart that takes a list of numbers (nums) and a value k as parameters. The function iterates over the list to find the maximum element and its index using a simple comparison. Then, it iterates over the list again to check if each element is either the maximum element or at most K positions away from the maximum element. If any element fails this condition, the function returns False. Otherwise, it returns True, indicating that all elements satisfy the condition.

Example 

Let's test the function with an example to see how it works −

nums = [5, 9, 7, 12, 9, 3, 7]
k = 2

result = test_k_apart(nums, k)
print(result)  # Output: True

In this example, the list nums contains elements that are at most 2 positions away from the maximum element (12), so the function returns True.

Output

True

Test Cases

To demonstrate the working of the program, let's consider some test cases with different input lists and values of k −

Test Case 1 

nums = [5, 2, 7, 1, 8]
k = 2

In this case, the maximum element in the list is 8. The elements are at the following positions relative to the maximum element: [3, 0, 1, 2, 0]. The absolute differences are [3, 0, 1, 2, 0]. Since all elements have absolute differences within k=2, the expected output is True.

Test Case 2 

nums = [10, 4, 5, 8, 2]
k = 1

In this case, the maximum element in the list is 10. The elements are at the following positions relative to the maximum element: [0, 1, 2, 1, 3]. The absolute differences are [0, 1, 2, 1, 3]. The element at index 4 (value 2) has an absolute difference of 3, which is greater than k=1. Therefore, the expected output is False.

Test Case 3 

nums = [3, 6, 9, 12, 15]
k = 3

In this case, the maximum element in the list is 15. The elements are at the following positions relative to the maximum element: [3, 2, 1, 0, 0]. The absolute differences are [3, 2, 1, 0, 0]. Since all elements have absolute differences within k=3, the expected output is True.

Conclusion

In this article, we discussed a Python program to test if all elements in a list are maximum of k positions apart from the maximum element. We explored an approach that leverages the concept of finding the maximum element and calculating the absolute differences of each element with respect to the maximum.

By using a simple loop and checking the absolute differences, we were able to determine whether all elements satisfy the given condition. By understanding and applying this program, you can effectively check if the elements in a list are within a specified range from the maximum element. This can be useful in various scenarios, such as validating data integrity or identifying patterns in sequences.

Updated on: 10-Aug-2023

40 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements