Move a Point

Given a point represented by a Point structure containing two integer fields:

  • x: The x-coordinate of the point.
  • y: The y-coordinate of the point.

You are also given two integers, changeX and changeY, representing the changes to be made to the point's coordinates.

Your task is to update the original point by adding changeX to its x-coordinate and changeY to its y-coordinate.

The point is passed by reference, so you must modify the original Point object directly rather than creating a separate point. The function returns no value, as the changes are made in place.

Note: The driver code constructs a Point structure from the supplied coordinates before calling movePoint()..

Example 1:

Input: p = {x: 3, y: 5}, changeX = 2, changeY = -1

Output: {x: 5, y: 4}

Explanation: Add 2 to the original x-coordinate: 3 + 2 = 5. Add -1 to the original y-coordinate: 5 + (-1) = 4. The original point is modified in place from (3, 5) to (5, 4).

Example 2:

Input: p = {x: -4, y: 7}, changeX = 4, changeY = 3

Output: {x: 0, y: 10}

Explanation: The updated x-coordinate is -4 + 4 = 0, and the updated y-coordinate is 7 + 3 = 10. These values are stored in the same point passed by reference, so its coordinates become (0, 10).

Now Your Turn!

Pick the correct output for the given input

Input: p = {x: 0, y: 0}, changeX = -5, changeY = -8

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • -109 <= p.x, p.y, changeX, changeY <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* };
*/
 
 
class Solution {
public:
void movePoint(Point& p, int changeX, int changeY) {
// Write your code here
}
};
 
Test Case

Input:

Change X
Change Y
P
X
Y