- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array called points containing the coordinate points on a 2D plane, they are sorted by the x-values, where points[i] = (x_i, y_i) so x_i < x_j for all 1 <= i < j <= number of points. We also have another value k. We have 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.

So, if the input is like points = [[2,4],[3,1],[6,11],[7,-9]] k = 1, then the output will be 6 because the first two points satisfy the condition |xi - xj| <= 1 now if we calculate the equation we get 4 + 1 + |2 - 3| = 6. Third and fourth points also satisfy the condition and returns a value of 11 + -9 + |6 - 7| = 3, so maximum is 6.

To solve this, we will follow these steps −

left := 0, right := 1

max_value := -inf

while right < size of points, do

(xl, yl) := points[left]

(xr, yr) := points[right]

diff := |xr - xl|

if left is same as right, then

right := right + 1

otherwise when diff <= k, then

m := yl + yr + diff

max_value := maximum of max_value and m

if yl >= yr - diff, then

right := right + 1

otherwise,

left := left + 1

otherwise,

left := left + 1

return max_value

Let us see the following implementation to get better understanding

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: m = yl + yr + diff max_value = max(max_value, m) if yl >= yr - diff: right += 1 else: left += 1 else: left += 1 return max_value points = [[2,4],[3,1],[6,11],[7,-9]] k = 1 print(solve(points, k))

[[2,4],[3,1],[6,11],[7,-9]], 1

6

- Related Questions & Answers
- Program to find out the value of a given equation in Python
- Program to find expected value of given equation for random numbers in Python
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- Program to find max number of K-sum pairs in Python
- Program to find max values of sublists of size k in Python
- C program to find the Roots of Quadratic equation
- C program to find the solution of linear equation
- Program to find number of solutions in Quadratic Equation in C++
- Java program to find the roots of a quadratic equation
- C++ Program to Find All Roots of a Quadratic Equation
- Java Program to Find all Roots of a Quadratic Equation
- Program to find max chunks to make array sorted in Python
- Python program to compute a Polynomial Equation
- Find the missing value from the given equation a + b = c in Python
- Program to find maximum possible value of an expression using given set of numbers in Python

Advertisements