Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check how many queries finds valid arithmetic sequence in Python
Suppose we have a list of numbers called nums and also have a list of queries. Each query element contains [i, j] asking whether the sublist of nums from index i to j (both inclusive) forms an arithmetic sequence. We need to count how many queries return true.
An arithmetic sequence has a constant difference between consecutive elements. For example, [2, 4, 6, 8] has a common difference of 2.
Example
If the input is nums = [2, 4, 6, 8, 7, 6, 5, 2] and queries = [[3, 4],[0, 3],[2, 4]], then the output will be 2 because ?
- Query
[0, 3]: sublist[2, 4, 6, 8]is arithmetic (difference = 2) ? True - Query
[3, 4]: sublist[8, 7]is arithmetic (difference = -1) ? True - Query
[2, 4]: sublist[6, 8, 7]is not arithmetic ? False
Approach
We use a Run-Length Encoding (RLE) technique to efficiently check arithmetic sequences ?
- Calculate differences between consecutive elements
- Use RLE to count consecutive equal differences
- For each query, check if the RLE count covers the required range
Implementation
def solve(nums, queries):
if not nums:
return 0
n = len(nums)
# Calculate differences between consecutive elements
diff = [nums[i + 1] - nums[i] for i in range(n - 1)]
# RLE: count consecutive equal differences
rle = [0] * (n - 1)
for i in range(n - 1):
if i > 0 and diff[i] == diff[i - 1]:
rle[i] = rle[i - 1] + 1
else:
rle[i] = 1
ans = 0
for i, j in queries:
if i == j: # Single element is always arithmetic
ans += 1
else:
# Check if RLE covers the required range
ans += rle[j - 1] >= (j - i)
return ans
# Test the function
nums = [2, 4, 6, 8, 7, 6, 5, 2]
queries = [[3, 4],[0, 3],[2, 4]]
result = solve(nums, queries)
print(f"Number of valid arithmetic sequences: {result}")
# Let's trace through the algorithm
print("\nStep-by-step breakdown:")
print(f"nums: {nums}")
print(f"differences: {[nums[i + 1] - nums[i] for i in range(len(nums) - 1)]}")
# Calculate RLE for visualization
diff = [nums[i + 1] - nums[i] for i in range(len(nums) - 1)]
rle = [0] * (len(nums) - 1)
for i in range(len(nums) - 1):
if i > 0 and diff[i] == diff[i - 1]:
rle[i] = rle[i - 1] + 1
else:
rle[i] = 1
print(f"RLE array: {rle}")
# Check each query
for i, (start, end) in enumerate(queries):
sublist = nums[start:end+1]
if start == end:
valid = True
else:
valid = rle[end - 1] >= (end - start)
print(f"Query {i+1} [{start}, {end}]: sublist {sublist} -> {valid}")
Number of valid arithmetic sequences: 2 Step-by-step breakdown: nums: [2, 4, 6, 8, 7, 6, 5, 2] differences: [2, 2, 2, -1, -1, -1, -3] RLE array: [1, 2, 3, 1, 2, 3, 1] Query 1 [3, 4]: sublist [8, 7] -> True Query 2 [0, 3]: sublist [2, 4, 6, 8] -> True Query 3 [2, 4]: sublist [6, 8, 7] -> False
How It Works
The RLE array stores the count of consecutive equal differences ending at each position. For a sublist from index i to j to be arithmetic, we need rle[j-1] >= (j-i), meaning the consecutive equal differences must span the entire range.
Time Complexity
Time: O(n + q) where n is the length of nums and q is the number of queries.
Space: O(n) for the difference and RLE arrays.
Conclusion
This solution efficiently checks arithmetic sequences using Run-Length Encoding to precompute consecutive equal differences. The algorithm handles edge cases like single-element queries and provides O(1) query time after O(n) preprocessing.
