Program to find the size of the longest sublist where car speed is constant in python

Suppose we have a list of numbers representing the position of a car at equally spaced intervals of time. We have to find the size of the longest sublist where the car was traveling at a constant speed.

So, if the input is like positions = [0, 4, 8, 12, 6, 4, 0], then the output will be 4, as the sublist is [0, 4, 8, 12].

Algorithm

To solve this, we will follow these steps −

  • j := 1
  • max_cnt := 0, current := 0
  • distance := |positions[0] - positions[1]|
  • while j < size of positions, do
    • prev := positions[j - 1]
    • if distance is same as |positions[j] - prev| , then
      • current := current + 1
    • otherwise,
      • max_cnt := maximum of max_cnt and current
      • current := 1
      • distance := |positions[j] - prev|
    • max_cnt := maximum of max_cnt and current
    • j := j + 1
  • return max_cnt + 1

Example

Let us see the following implementation to get better understanding −

class Solution:
    def solve(self, positions):
        j = 1
        max_cnt = 0
        current = 0
        distance = abs(positions[0] - positions[1])
        while j < len(positions):
            prev = positions[j - 1]
            if distance == abs(positions[j] - prev):
                current += 1
            else:
                max_cnt = max(max_cnt, current)
                current = 1
                distance = abs(positions[j] - prev)
            max_cnt = max(max_cnt, current)
            j += 1
        return max_cnt + 1

ob = Solution()
positions = [0, 4, 8, 12, 6, 4, 0]
print(ob.solve(positions))
4

How It Works

The algorithm calculates the distance between consecutive positions to determine constant speed. When positions change by the same distance at each time interval, the car maintains constant speed. The car moves at constant speed in sublist [0, 4, 8, 12] with distance 4 between consecutive positions.

Alternative Approach

Here's a more readable implementation using a function approach −

def longest_constant_speed(positions):
    if len(positions) < 2:
        return len(positions)
    
    max_length = 1
    current_length = 1
    current_distance = abs(positions[1] - positions[0])
    
    for i in range(2, len(positions)):
        distance = abs(positions[i] - positions[i-1])
        if distance == current_distance:
            current_length += 1
        else:
            max_length = max(max_length, current_length)
            current_length = 2
            current_distance = distance
    
    return max(max_length, current_length)

# Test with the example
positions = [0, 4, 8, 12, 6, 4, 0]
result = longest_constant_speed(positions)
print(f"Longest constant speed sublist length: {result}")
Longest constant speed sublist length: 4

Conclusion

The solution tracks consecutive positions with equal distance differences to find the longest constant speed segment. Both approaches have O(n) time complexity and efficiently identify the maximum sublist length where car speed remains constant.

Updated on: 2026-03-25T12:57:11+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements