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 find max value of an equation in Python
Suppose we have an array called points containing coordinate points on a 2D plane, sorted by x-values, where points[i] = (x_i, y_i) and x_i < x_j for all 1 <= i < j <= number of points. We also have a value k. We need to find the maximum value of the equation y_i + y_j + |x_i - x_j| where |x_i - x_j| <= k and 1 <= i < j <= number of points.
For example, if points = [[2,4],[3,1],[6,11],[7,-9]] and k = 1, the output will be 6 because the first two points satisfy |2 - 3| = 1 <= 1, and the equation gives us 4 + 1 + 1 = 6. The third and fourth points also satisfy the condition with 11 + (-9) + 1 = 3, so the maximum is 6.
Algorithm
To solve this problem, we use a two-pointer approach ?
Initialize
left = 0andright = 1Set
max_value = -infinity-
While
right < size of points:Get coordinates
(xl, yl) = points[left]and(xr, yr) = points[right]Calculate
diff = |xr - xl|If
left == right, incrementrightElse if
diff <= k, calculate equation value and updatemax_valueElse increment
left
Return
max_value
Implementation
def solve(points, k):
left, right = 0, 1
max_value = float('-inf')
while right < len(points):
xl, yl = points[left]
xr, yr = points[right]
diff = abs(xr - xl)
if left == right:
right += 1
elif diff <= k:
equation_value = yl + yr + diff
max_value = max(max_value, equation_value)
# Optimize pointer movement
if yl >= yr - diff:
right += 1
else:
left += 1
else:
left += 1
return max_value
# Test with example
points = [[2,4],[3,1],[6,11],[7,-9]]
k = 1
result = solve(points, k)
print(f"Maximum equation value: {result}")
Maximum equation value: 6
How It Works
The algorithm uses two pointers to efficiently check all valid pairs. Since points are sorted by x-coordinates, we can optimize the search:
When the distance constraint is satisfied, we calculate the equation value
We intelligently move pointers based on which point contributes more to the equation
If
yl >= yr - diff, the left point is more valuable, so we try the next right pointOtherwise, we move the left pointer to find a better left point
Example Walkthrough
def solve_with_steps(points, k):
left, right = 0, 1
max_value = float('-inf')
print(f"Points: {points}, k = {k}")
print("Step-by-step execution:")
while right < len(points):
xl, yl = points[left]
xr, yr = points[right]
diff = abs(xr - xl)
print(f" Checking points[{left}]={points[left]} and points[{right}]={points[right]}")
print(f" Distance: |{xr} - {xl}| = {diff}")
if left == right:
right += 1
elif diff <= k:
equation_value = yl + yr + diff
print(f" Equation: {yl} + {yr} + {diff} = {equation_value}")
max_value = max(max_value, equation_value)
print(f" Current max: {max_value}")
if yl >= yr - diff:
right += 1
print(f" Moving right pointer to {right}")
else:
left += 1
print(f" Moving left pointer to {left}")
else:
left += 1
print(f" Distance > k, moving left pointer to {left}")
print()
return max_value
points = [[2,4],[3,1],[6,11],[7,-9]]
k = 1
result = solve_with_steps(points, k)
print(f"Final result: {result}")
Points: [[2, 4], [3, 1], [6, 11], [7, -9]], k = 1 Step-by-step execution: Checking points[0]=[2, 4] and points[1]=[3, 1] Distance: |3 - 2| = 1 Equation: 4 + 1 + 1 = 6 Current max: 6 Moving right pointer to 2 Checking points[0]=[2, 4] and points[2]=[6, 11] Distance: |6 - 2| = 4 Distance > k, moving left pointer to 1 Checking points[1]=[3, 1] and points[2]=[6, 11] Distance: |6 - 3| = 3 Distance > k, moving left pointer to 2 Checking points[2]=[6, 11] and points[2]=[6, 11] Moving right pointer to 3 Checking points[2]=[6, 11] and points[3]=[7, -9] Distance: |7 - 6| = 1 Equation: 11 + -9 + 1 = 3 Current max: 6 Moving right pointer to 4 Final result: 6
Conclusion
This two-pointer approach efficiently finds the maximum equation value in O(n) time complexity. The key insight is using the sorted nature of points and intelligent pointer movement to avoid checking all possible pairs.
